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 13f094fea6SMark Brown #include <linux/bsearch.h> 14e39be3a3SXiubo Li #include <linux/device.h> 15e39be3a3SXiubo Li #include <linux/export.h> 16e39be3a3SXiubo Li #include <linux/slab.h> 17c08604b8SDimitris Papastamos #include <linux/sort.h> 189fabe24eSDimitris Papastamos 19f58078daSSteven Rostedt #include "trace.h" 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 39fb70067eSXiubo Li /* calculate the size of reg_defaults */ 40fb70067eSXiubo Li for (count = 0, i = 0; i < map->num_reg_defaults_raw; i++) 41fb70067eSXiubo Li if (!regmap_volatile(map, i * map->reg_stride)) 42fb70067eSXiubo Li count++; 43fb70067eSXiubo Li 44fb70067eSXiubo Li /* all registers are volatile, so just bypass */ 45fb70067eSXiubo Li if (!count) { 46fb70067eSXiubo Li map->cache_bypass = true; 47fb70067eSXiubo Li return 0; 48fb70067eSXiubo Li } 49fb70067eSXiubo Li 50fb70067eSXiubo Li map->num_reg_defaults = count; 51fb70067eSXiubo Li map->reg_defaults = kmalloc_array(count, sizeof(struct reg_default), 52fb70067eSXiubo Li GFP_KERNEL); 53fb70067eSXiubo Li if (!map->reg_defaults) 54fb70067eSXiubo Li return -ENOMEM; 55fb70067eSXiubo Li 569fabe24eSDimitris Papastamos if (!map->reg_defaults_raw) { 57df00c79fSLaxman Dewangan u32 cache_bypass = map->cache_bypass; 589fabe24eSDimitris Papastamos dev_warn(map->dev, "No cache defaults, reading back from HW\n"); 59df00c79fSLaxman Dewangan 60df00c79fSLaxman Dewangan /* Bypass the cache access till data read from HW*/ 61df00c79fSLaxman Dewangan map->cache_bypass = 1; 629fabe24eSDimitris Papastamos tmp_buf = kmalloc(map->cache_size_raw, GFP_KERNEL); 63fb70067eSXiubo Li if (!tmp_buf) { 64fb70067eSXiubo Li ret = -ENOMEM; 65fb70067eSXiubo Li goto err_free; 66fb70067eSXiubo Li } 67eb4cb76fSMark Brown ret = regmap_raw_read(map, 0, tmp_buf, 689fabe24eSDimitris Papastamos map->num_reg_defaults_raw); 69df00c79fSLaxman Dewangan map->cache_bypass = cache_bypass; 70fb70067eSXiubo Li if (ret < 0) 71fb70067eSXiubo Li goto err_cache_free; 72fb70067eSXiubo Li 739fabe24eSDimitris Papastamos map->reg_defaults_raw = tmp_buf; 749fabe24eSDimitris Papastamos map->cache_free = 1; 759fabe24eSDimitris Papastamos } 769fabe24eSDimitris Papastamos 779fabe24eSDimitris Papastamos /* fill the reg_defaults */ 789fabe24eSDimitris Papastamos for (i = 0, j = 0; i < map->num_reg_defaults_raw; i++) { 79f01ee60fSStephen Warren if (regmap_volatile(map, i * map->reg_stride)) 809fabe24eSDimitris Papastamos continue; 81fbba43c5SXiubo Li val = regcache_get_val(map, map->reg_defaults_raw, i); 82f01ee60fSStephen Warren map->reg_defaults[j].reg = i * map->reg_stride; 839fabe24eSDimitris Papastamos map->reg_defaults[j].def = val; 849fabe24eSDimitris Papastamos j++; 859fabe24eSDimitris Papastamos } 869fabe24eSDimitris Papastamos 879fabe24eSDimitris Papastamos return 0; 88021cd616SLars-Peter Clausen 89fb70067eSXiubo Li err_cache_free: 90fb70067eSXiubo Li kfree(tmp_buf); 91021cd616SLars-Peter Clausen err_free: 92fb70067eSXiubo Li kfree(map->reg_defaults); 93021cd616SLars-Peter Clausen 94021cd616SLars-Peter Clausen return ret; 959fabe24eSDimitris Papastamos } 969fabe24eSDimitris Papastamos 97e5e3b8abSLars-Peter Clausen int regcache_init(struct regmap *map, const struct regmap_config *config) 989fabe24eSDimitris Papastamos { 999fabe24eSDimitris Papastamos int ret; 1009fabe24eSDimitris Papastamos int i; 1019fabe24eSDimitris Papastamos void *tmp_buf; 1029fabe24eSDimitris Papastamos 103f01ee60fSStephen Warren for (i = 0; i < config->num_reg_defaults; i++) 104f01ee60fSStephen Warren if (config->reg_defaults[i].reg % map->reg_stride) 105f01ee60fSStephen Warren return -EINVAL; 106f01ee60fSStephen Warren 107e7a6db30SMark Brown if (map->cache_type == REGCACHE_NONE) { 108e7a6db30SMark Brown map->cache_bypass = true; 1099fabe24eSDimitris Papastamos return 0; 110e7a6db30SMark Brown } 1119fabe24eSDimitris Papastamos 1129fabe24eSDimitris Papastamos for (i = 0; i < ARRAY_SIZE(cache_types); i++) 1139fabe24eSDimitris Papastamos if (cache_types[i]->type == map->cache_type) 1149fabe24eSDimitris Papastamos break; 1159fabe24eSDimitris Papastamos 1169fabe24eSDimitris Papastamos if (i == ARRAY_SIZE(cache_types)) { 1179fabe24eSDimitris Papastamos dev_err(map->dev, "Could not match compress type: %d\n", 1189fabe24eSDimitris Papastamos map->cache_type); 1199fabe24eSDimitris Papastamos return -EINVAL; 1209fabe24eSDimitris Papastamos } 1219fabe24eSDimitris Papastamos 122e5e3b8abSLars-Peter Clausen map->num_reg_defaults = config->num_reg_defaults; 123e5e3b8abSLars-Peter Clausen map->num_reg_defaults_raw = config->num_reg_defaults_raw; 124e5e3b8abSLars-Peter Clausen map->reg_defaults_raw = config->reg_defaults_raw; 125064d4db1SLars-Peter Clausen map->cache_word_size = DIV_ROUND_UP(config->val_bits, 8); 126064d4db1SLars-Peter Clausen map->cache_size_raw = map->cache_word_size * config->num_reg_defaults_raw; 127e5e3b8abSLars-Peter Clausen 1289fabe24eSDimitris Papastamos map->cache = NULL; 1299fabe24eSDimitris Papastamos map->cache_ops = cache_types[i]; 1309fabe24eSDimitris Papastamos 1319fabe24eSDimitris Papastamos if (!map->cache_ops->read || 1329fabe24eSDimitris Papastamos !map->cache_ops->write || 1339fabe24eSDimitris Papastamos !map->cache_ops->name) 1349fabe24eSDimitris Papastamos return -EINVAL; 1359fabe24eSDimitris Papastamos 1369fabe24eSDimitris Papastamos /* We still need to ensure that the reg_defaults 1379fabe24eSDimitris Papastamos * won't vanish from under us. We'll need to make 1389fabe24eSDimitris Papastamos * a copy of it. 1399fabe24eSDimitris Papastamos */ 140720e4616SLars-Peter Clausen if (config->reg_defaults) { 1419fabe24eSDimitris Papastamos if (!map->num_reg_defaults) 1429fabe24eSDimitris Papastamos return -EINVAL; 143720e4616SLars-Peter Clausen tmp_buf = kmemdup(config->reg_defaults, map->num_reg_defaults * 1449fabe24eSDimitris Papastamos sizeof(struct reg_default), GFP_KERNEL); 1459fabe24eSDimitris Papastamos if (!tmp_buf) 1469fabe24eSDimitris Papastamos return -ENOMEM; 1479fabe24eSDimitris Papastamos map->reg_defaults = tmp_buf; 1488528bdd4SMark Brown } else if (map->num_reg_defaults_raw) { 1495fcd2560SMark Brown /* Some devices such as PMICs don't have cache defaults, 1509fabe24eSDimitris Papastamos * we cope with this by reading back the HW registers and 1519fabe24eSDimitris Papastamos * crafting the cache defaults by hand. 1529fabe24eSDimitris Papastamos */ 1539fabe24eSDimitris Papastamos ret = regcache_hw_init(map); 1549fabe24eSDimitris Papastamos if (ret < 0) 1559fabe24eSDimitris Papastamos return ret; 156fb70067eSXiubo Li if (map->cache_bypass) 157fb70067eSXiubo Li return 0; 1589fabe24eSDimitris Papastamos } 1599fabe24eSDimitris Papastamos 1609fabe24eSDimitris Papastamos if (!map->max_register) 1619fabe24eSDimitris Papastamos map->max_register = map->num_reg_defaults_raw; 1629fabe24eSDimitris Papastamos 1639fabe24eSDimitris Papastamos if (map->cache_ops->init) { 1649fabe24eSDimitris Papastamos dev_dbg(map->dev, "Initializing %s cache\n", 1659fabe24eSDimitris Papastamos map->cache_ops->name); 166bd061c78SLars-Peter Clausen ret = map->cache_ops->init(map); 167bd061c78SLars-Peter Clausen if (ret) 168bd061c78SLars-Peter Clausen goto err_free; 1699fabe24eSDimitris Papastamos } 1709fabe24eSDimitris Papastamos return 0; 171bd061c78SLars-Peter Clausen 172bd061c78SLars-Peter Clausen err_free: 173bd061c78SLars-Peter Clausen kfree(map->reg_defaults); 174bd061c78SLars-Peter Clausen if (map->cache_free) 175bd061c78SLars-Peter Clausen kfree(map->reg_defaults_raw); 176bd061c78SLars-Peter Clausen 177bd061c78SLars-Peter Clausen return ret; 1789fabe24eSDimitris Papastamos } 1799fabe24eSDimitris Papastamos 1809fabe24eSDimitris Papastamos void regcache_exit(struct regmap *map) 1819fabe24eSDimitris Papastamos { 1829fabe24eSDimitris Papastamos if (map->cache_type == REGCACHE_NONE) 1839fabe24eSDimitris Papastamos return; 1849fabe24eSDimitris Papastamos 1859fabe24eSDimitris Papastamos BUG_ON(!map->cache_ops); 1869fabe24eSDimitris Papastamos 1879fabe24eSDimitris Papastamos kfree(map->reg_defaults); 1889fabe24eSDimitris Papastamos if (map->cache_free) 1899fabe24eSDimitris Papastamos kfree(map->reg_defaults_raw); 1909fabe24eSDimitris Papastamos 1919fabe24eSDimitris Papastamos if (map->cache_ops->exit) { 1929fabe24eSDimitris Papastamos dev_dbg(map->dev, "Destroying %s cache\n", 1939fabe24eSDimitris Papastamos map->cache_ops->name); 1949fabe24eSDimitris Papastamos map->cache_ops->exit(map); 1959fabe24eSDimitris Papastamos } 1969fabe24eSDimitris Papastamos } 1979fabe24eSDimitris Papastamos 1989fabe24eSDimitris Papastamos /** 1999fabe24eSDimitris Papastamos * regcache_read: Fetch the value of a given register from the cache. 2009fabe24eSDimitris Papastamos * 2019fabe24eSDimitris Papastamos * @map: map to configure. 2029fabe24eSDimitris Papastamos * @reg: The register index. 2039fabe24eSDimitris Papastamos * @value: The value to be returned. 2049fabe24eSDimitris Papastamos * 2059fabe24eSDimitris Papastamos * Return a negative value on failure, 0 on success. 2069fabe24eSDimitris Papastamos */ 2079fabe24eSDimitris Papastamos int regcache_read(struct regmap *map, 2089fabe24eSDimitris Papastamos unsigned int reg, unsigned int *value) 2099fabe24eSDimitris Papastamos { 210bc7ee556SMark Brown int ret; 211bc7ee556SMark Brown 2129fabe24eSDimitris Papastamos if (map->cache_type == REGCACHE_NONE) 2139fabe24eSDimitris Papastamos return -ENOSYS; 2149fabe24eSDimitris Papastamos 2159fabe24eSDimitris Papastamos BUG_ON(!map->cache_ops); 2169fabe24eSDimitris Papastamos 217bc7ee556SMark Brown if (!regmap_volatile(map, reg)) { 218bc7ee556SMark Brown ret = map->cache_ops->read(map, reg, value); 219bc7ee556SMark Brown 220bc7ee556SMark Brown if (ret == 0) 221c6b570d9SPhilipp Zabel trace_regmap_reg_read_cache(map, reg, *value); 222bc7ee556SMark Brown 223bc7ee556SMark Brown return ret; 224bc7ee556SMark Brown } 2259fabe24eSDimitris Papastamos 2269fabe24eSDimitris Papastamos return -EINVAL; 2279fabe24eSDimitris Papastamos } 2289fabe24eSDimitris Papastamos 2299fabe24eSDimitris Papastamos /** 2309fabe24eSDimitris Papastamos * regcache_write: Set the value of a given register in the cache. 2319fabe24eSDimitris Papastamos * 2329fabe24eSDimitris Papastamos * @map: map to configure. 2339fabe24eSDimitris Papastamos * @reg: The register index. 2349fabe24eSDimitris Papastamos * @value: The new register value. 2359fabe24eSDimitris Papastamos * 2369fabe24eSDimitris Papastamos * Return a negative value on failure, 0 on success. 2379fabe24eSDimitris Papastamos */ 2389fabe24eSDimitris Papastamos int regcache_write(struct regmap *map, 2399fabe24eSDimitris Papastamos unsigned int reg, unsigned int value) 2409fabe24eSDimitris Papastamos { 2419fabe24eSDimitris Papastamos if (map->cache_type == REGCACHE_NONE) 2429fabe24eSDimitris Papastamos return 0; 2439fabe24eSDimitris Papastamos 2449fabe24eSDimitris Papastamos BUG_ON(!map->cache_ops); 2459fabe24eSDimitris Papastamos 2469fabe24eSDimitris Papastamos if (!regmap_volatile(map, reg)) 2479fabe24eSDimitris Papastamos return map->cache_ops->write(map, reg, value); 2489fabe24eSDimitris Papastamos 2499fabe24eSDimitris Papastamos return 0; 2509fabe24eSDimitris Papastamos } 2519fabe24eSDimitris Papastamos 2523969fa08SKevin Cernekee static bool regcache_reg_needs_sync(struct regmap *map, unsigned int reg, 2533969fa08SKevin Cernekee unsigned int val) 2543969fa08SKevin Cernekee { 2553969fa08SKevin Cernekee int ret; 2563969fa08SKevin Cernekee 257*1c79771aSKevin Cernekee /* If we don't know the chip just got reset, then sync everything. */ 258*1c79771aSKevin Cernekee if (!map->no_sync_defaults) 259*1c79771aSKevin Cernekee return true; 260*1c79771aSKevin Cernekee 2613969fa08SKevin Cernekee /* Is this the hardware default? If so skip. */ 2623969fa08SKevin Cernekee ret = regcache_lookup_reg(map, reg); 2633969fa08SKevin Cernekee if (ret >= 0 && val == map->reg_defaults[ret].def) 2643969fa08SKevin Cernekee return false; 2653969fa08SKevin Cernekee return true; 2663969fa08SKevin Cernekee } 2673969fa08SKevin Cernekee 268d856fce4SMaarten ter Huurne static int regcache_default_sync(struct regmap *map, unsigned int min, 269d856fce4SMaarten ter Huurne unsigned int max) 270d856fce4SMaarten ter Huurne { 271d856fce4SMaarten ter Huurne unsigned int reg; 272d856fce4SMaarten ter Huurne 27375617328SDylan Reid for (reg = min; reg <= max; reg += map->reg_stride) { 274d856fce4SMaarten ter Huurne unsigned int val; 275d856fce4SMaarten ter Huurne int ret; 276d856fce4SMaarten ter Huurne 27783f8475cSDylan Reid if (regmap_volatile(map, reg) || 27883f8475cSDylan Reid !regmap_writeable(map, reg)) 279d856fce4SMaarten ter Huurne continue; 280d856fce4SMaarten ter Huurne 281d856fce4SMaarten ter Huurne ret = regcache_read(map, reg, &val); 282d856fce4SMaarten ter Huurne if (ret) 283d856fce4SMaarten ter Huurne return ret; 284d856fce4SMaarten ter Huurne 2853969fa08SKevin Cernekee if (!regcache_reg_needs_sync(map, reg, val)) 286d856fce4SMaarten ter Huurne continue; 287d856fce4SMaarten ter Huurne 288d856fce4SMaarten ter Huurne map->cache_bypass = 1; 289d856fce4SMaarten ter Huurne ret = _regmap_write(map, reg, val); 290d856fce4SMaarten ter Huurne map->cache_bypass = 0; 291f29a4320SJarkko Nikula if (ret) { 292f29a4320SJarkko Nikula dev_err(map->dev, "Unable to sync register %#x. %d\n", 293f29a4320SJarkko Nikula reg, ret); 294d856fce4SMaarten ter Huurne return ret; 295f29a4320SJarkko Nikula } 296d856fce4SMaarten ter Huurne dev_dbg(map->dev, "Synced register %#x, value %#x\n", reg, val); 297d856fce4SMaarten ter Huurne } 298d856fce4SMaarten ter Huurne 299d856fce4SMaarten ter Huurne return 0; 300d856fce4SMaarten ter Huurne } 301d856fce4SMaarten ter Huurne 3029fabe24eSDimitris Papastamos /** 3039fabe24eSDimitris Papastamos * regcache_sync: Sync the register cache with the hardware. 3049fabe24eSDimitris Papastamos * 3059fabe24eSDimitris Papastamos * @map: map to configure. 3069fabe24eSDimitris Papastamos * 3079fabe24eSDimitris Papastamos * Any registers that should not be synced should be marked as 3089fabe24eSDimitris Papastamos * volatile. In general drivers can choose not to use the provided 3099fabe24eSDimitris Papastamos * syncing functionality if they so require. 3109fabe24eSDimitris Papastamos * 3119fabe24eSDimitris Papastamos * Return a negative value on failure, 0 on success. 3129fabe24eSDimitris Papastamos */ 3139fabe24eSDimitris Papastamos int regcache_sync(struct regmap *map) 3149fabe24eSDimitris Papastamos { 315954757d7SDimitris Papastamos int ret = 0; 316954757d7SDimitris Papastamos unsigned int i; 31759360089SDimitris Papastamos const char *name; 318beb1a10fSDimitris Papastamos unsigned int bypass; 31959360089SDimitris Papastamos 320d856fce4SMaarten ter Huurne BUG_ON(!map->cache_ops); 3219fabe24eSDimitris Papastamos 32281485f52SLars-Peter Clausen map->lock(map->lock_arg); 323beb1a10fSDimitris Papastamos /* Remember the initial bypass state */ 324beb1a10fSDimitris Papastamos bypass = map->cache_bypass; 3259fabe24eSDimitris Papastamos dev_dbg(map->dev, "Syncing %s cache\n", 3269fabe24eSDimitris Papastamos map->cache_ops->name); 32759360089SDimitris Papastamos name = map->cache_ops->name; 328c6b570d9SPhilipp Zabel trace_regcache_sync(map, name, "start"); 32922f0d90aSMark Brown 3308ae0d7e8SMark Brown if (!map->cache_dirty) 3318ae0d7e8SMark Brown goto out; 332d9db7627SMark Brown 333affbe886SMark Brown map->async = true; 334affbe886SMark Brown 33522f0d90aSMark Brown /* Apply any patch first */ 3368a892d69SMark Brown map->cache_bypass = 1; 33722f0d90aSMark Brown for (i = 0; i < map->patch_regs; i++) { 33822f0d90aSMark Brown ret = _regmap_write(map, map->patch[i].reg, map->patch[i].def); 33922f0d90aSMark Brown if (ret != 0) { 34022f0d90aSMark Brown dev_err(map->dev, "Failed to write %x = %x: %d\n", 34122f0d90aSMark Brown map->patch[i].reg, map->patch[i].def, ret); 34222f0d90aSMark Brown goto out; 34322f0d90aSMark Brown } 34422f0d90aSMark Brown } 3458a892d69SMark Brown map->cache_bypass = 0; 34622f0d90aSMark Brown 347d856fce4SMaarten ter Huurne if (map->cache_ops->sync) 348ac8d91c8SMark Brown ret = map->cache_ops->sync(map, 0, map->max_register); 349d856fce4SMaarten ter Huurne else 350d856fce4SMaarten ter Huurne ret = regcache_default_sync(map, 0, map->max_register); 351954757d7SDimitris Papastamos 3526ff73738SMark Brown if (ret == 0) 3536ff73738SMark Brown map->cache_dirty = false; 3546ff73738SMark Brown 355954757d7SDimitris Papastamos out: 356beb1a10fSDimitris Papastamos /* Restore the bypass state */ 357affbe886SMark Brown map->async = false; 358beb1a10fSDimitris Papastamos map->cache_bypass = bypass; 359*1c79771aSKevin Cernekee map->no_sync_defaults = false; 36081485f52SLars-Peter Clausen map->unlock(map->lock_arg); 361954757d7SDimitris Papastamos 362affbe886SMark Brown regmap_async_complete(map); 363affbe886SMark Brown 364c6b570d9SPhilipp Zabel trace_regcache_sync(map, name, "stop"); 365affbe886SMark Brown 366954757d7SDimitris Papastamos return ret; 3679fabe24eSDimitris Papastamos } 3689fabe24eSDimitris Papastamos EXPORT_SYMBOL_GPL(regcache_sync); 3699fabe24eSDimitris Papastamos 37092afb286SMark Brown /** 3714d4cfd16SMark Brown * regcache_sync_region: Sync part of the register cache with the hardware. 3724d4cfd16SMark Brown * 3734d4cfd16SMark Brown * @map: map to sync. 3744d4cfd16SMark Brown * @min: first register to sync 3754d4cfd16SMark Brown * @max: last register to sync 3764d4cfd16SMark Brown * 3774d4cfd16SMark Brown * Write all non-default register values in the specified region to 3784d4cfd16SMark Brown * the hardware. 3794d4cfd16SMark Brown * 3804d4cfd16SMark Brown * Return a negative value on failure, 0 on success. 3814d4cfd16SMark Brown */ 3824d4cfd16SMark Brown int regcache_sync_region(struct regmap *map, unsigned int min, 3834d4cfd16SMark Brown unsigned int max) 3844d4cfd16SMark Brown { 3854d4cfd16SMark Brown int ret = 0; 3864d4cfd16SMark Brown const char *name; 3874d4cfd16SMark Brown unsigned int bypass; 3884d4cfd16SMark Brown 389d856fce4SMaarten ter Huurne BUG_ON(!map->cache_ops); 3904d4cfd16SMark Brown 39181485f52SLars-Peter Clausen map->lock(map->lock_arg); 3924d4cfd16SMark Brown 3934d4cfd16SMark Brown /* Remember the initial bypass state */ 3944d4cfd16SMark Brown bypass = map->cache_bypass; 3954d4cfd16SMark Brown 3964d4cfd16SMark Brown name = map->cache_ops->name; 3974d4cfd16SMark Brown dev_dbg(map->dev, "Syncing %s cache from %d-%d\n", name, min, max); 3984d4cfd16SMark Brown 399c6b570d9SPhilipp Zabel trace_regcache_sync(map, name, "start region"); 4004d4cfd16SMark Brown 4014d4cfd16SMark Brown if (!map->cache_dirty) 4024d4cfd16SMark Brown goto out; 4034d4cfd16SMark Brown 404affbe886SMark Brown map->async = true; 405affbe886SMark Brown 406d856fce4SMaarten ter Huurne if (map->cache_ops->sync) 4074d4cfd16SMark Brown ret = map->cache_ops->sync(map, min, max); 408d856fce4SMaarten ter Huurne else 409d856fce4SMaarten ter Huurne ret = regcache_default_sync(map, min, max); 4104d4cfd16SMark Brown 4114d4cfd16SMark Brown out: 4124d4cfd16SMark Brown /* Restore the bypass state */ 4134d4cfd16SMark Brown map->cache_bypass = bypass; 414affbe886SMark Brown map->async = false; 415*1c79771aSKevin Cernekee map->no_sync_defaults = false; 41681485f52SLars-Peter Clausen map->unlock(map->lock_arg); 4174d4cfd16SMark Brown 418affbe886SMark Brown regmap_async_complete(map); 419affbe886SMark Brown 420c6b570d9SPhilipp Zabel trace_regcache_sync(map, name, "stop region"); 421affbe886SMark Brown 4224d4cfd16SMark Brown return ret; 4234d4cfd16SMark Brown } 424e466de05SMark Brown EXPORT_SYMBOL_GPL(regcache_sync_region); 4254d4cfd16SMark Brown 4264d4cfd16SMark Brown /** 427697e85bcSMark Brown * regcache_drop_region: Discard part of the register cache 428697e85bcSMark Brown * 429697e85bcSMark Brown * @map: map to operate on 430697e85bcSMark Brown * @min: first register to discard 431697e85bcSMark Brown * @max: last register to discard 432697e85bcSMark Brown * 433697e85bcSMark Brown * Discard part of the register cache. 434697e85bcSMark Brown * 435697e85bcSMark Brown * Return a negative value on failure, 0 on success. 436697e85bcSMark Brown */ 437697e85bcSMark Brown int regcache_drop_region(struct regmap *map, unsigned int min, 438697e85bcSMark Brown unsigned int max) 439697e85bcSMark Brown { 440697e85bcSMark Brown int ret = 0; 441697e85bcSMark Brown 4423f4ff561SLars-Peter Clausen if (!map->cache_ops || !map->cache_ops->drop) 443697e85bcSMark Brown return -EINVAL; 444697e85bcSMark Brown 44581485f52SLars-Peter Clausen map->lock(map->lock_arg); 446697e85bcSMark Brown 447c6b570d9SPhilipp Zabel trace_regcache_drop_region(map, min, max); 448697e85bcSMark Brown 449697e85bcSMark Brown ret = map->cache_ops->drop(map, min, max); 450697e85bcSMark Brown 45181485f52SLars-Peter Clausen map->unlock(map->lock_arg); 452697e85bcSMark Brown 453697e85bcSMark Brown return ret; 454697e85bcSMark Brown } 455697e85bcSMark Brown EXPORT_SYMBOL_GPL(regcache_drop_region); 456697e85bcSMark Brown 457697e85bcSMark Brown /** 45892afb286SMark Brown * regcache_cache_only: Put a register map into cache only mode 45992afb286SMark Brown * 46092afb286SMark Brown * @map: map to configure 46192afb286SMark Brown * @cache_only: flag if changes should be written to the hardware 46292afb286SMark Brown * 46392afb286SMark Brown * When a register map is marked as cache only writes to the register 46492afb286SMark Brown * map API will only update the register cache, they will not cause 46592afb286SMark Brown * any hardware changes. This is useful for allowing portions of 46692afb286SMark Brown * drivers to act as though the device were functioning as normal when 46792afb286SMark Brown * it is disabled for power saving reasons. 46892afb286SMark Brown */ 46992afb286SMark Brown void regcache_cache_only(struct regmap *map, bool enable) 47092afb286SMark Brown { 47181485f52SLars-Peter Clausen map->lock(map->lock_arg); 472ac77a765SDimitris Papastamos WARN_ON(map->cache_bypass && enable); 47392afb286SMark Brown map->cache_only = enable; 474c6b570d9SPhilipp Zabel trace_regmap_cache_only(map, enable); 47581485f52SLars-Peter Clausen map->unlock(map->lock_arg); 47692afb286SMark Brown } 47792afb286SMark Brown EXPORT_SYMBOL_GPL(regcache_cache_only); 47892afb286SMark Brown 4796eb0f5e0SDimitris Papastamos /** 480*1c79771aSKevin Cernekee * regcache_mark_dirty: Indicate that HW registers were reset to default values 4818ae0d7e8SMark Brown * 4828ae0d7e8SMark Brown * @map: map to mark 4838ae0d7e8SMark Brown * 484*1c79771aSKevin Cernekee * Inform regcache that the device has been powered down or reset, so that 485*1c79771aSKevin Cernekee * on resume, regcache_sync() knows to write out all non-default values 486*1c79771aSKevin Cernekee * stored in the cache. 487*1c79771aSKevin Cernekee * 488*1c79771aSKevin Cernekee * If this function is not called, regcache_sync() will assume that 489*1c79771aSKevin Cernekee * the hardware state still matches the cache state, modulo any writes that 490*1c79771aSKevin Cernekee * happened when cache_only was true. 4918ae0d7e8SMark Brown */ 4928ae0d7e8SMark Brown void regcache_mark_dirty(struct regmap *map) 4938ae0d7e8SMark Brown { 49481485f52SLars-Peter Clausen map->lock(map->lock_arg); 4958ae0d7e8SMark Brown map->cache_dirty = true; 496*1c79771aSKevin Cernekee map->no_sync_defaults = true; 49781485f52SLars-Peter Clausen map->unlock(map->lock_arg); 4988ae0d7e8SMark Brown } 4998ae0d7e8SMark Brown EXPORT_SYMBOL_GPL(regcache_mark_dirty); 5008ae0d7e8SMark Brown 5018ae0d7e8SMark Brown /** 5026eb0f5e0SDimitris Papastamos * regcache_cache_bypass: Put a register map into cache bypass mode 5036eb0f5e0SDimitris Papastamos * 5046eb0f5e0SDimitris Papastamos * @map: map to configure 5050eef6b04SDimitris Papastamos * @cache_bypass: flag if changes should not be written to the hardware 5066eb0f5e0SDimitris Papastamos * 5076eb0f5e0SDimitris Papastamos * When a register map is marked with the cache bypass option, writes 5086eb0f5e0SDimitris Papastamos * to the register map API will only update the hardware and not the 5096eb0f5e0SDimitris Papastamos * the cache directly. This is useful when syncing the cache back to 5106eb0f5e0SDimitris Papastamos * the hardware. 5116eb0f5e0SDimitris Papastamos */ 5126eb0f5e0SDimitris Papastamos void regcache_cache_bypass(struct regmap *map, bool enable) 5136eb0f5e0SDimitris Papastamos { 51481485f52SLars-Peter Clausen map->lock(map->lock_arg); 515ac77a765SDimitris Papastamos WARN_ON(map->cache_only && enable); 5166eb0f5e0SDimitris Papastamos map->cache_bypass = enable; 517c6b570d9SPhilipp Zabel trace_regmap_cache_bypass(map, enable); 51881485f52SLars-Peter Clausen map->unlock(map->lock_arg); 5196eb0f5e0SDimitris Papastamos } 5206eb0f5e0SDimitris Papastamos EXPORT_SYMBOL_GPL(regcache_cache_bypass); 5216eb0f5e0SDimitris Papastamos 522879082c9SMark Brown bool regcache_set_val(struct regmap *map, void *base, unsigned int idx, 523879082c9SMark Brown unsigned int val) 5249fabe24eSDimitris Papastamos { 525325acab4SMark Brown if (regcache_get_val(map, base, idx) == val) 526325acab4SMark Brown return true; 527325acab4SMark Brown 528eb4cb76fSMark Brown /* Use device native format if possible */ 529eb4cb76fSMark Brown if (map->format.format_val) { 530eb4cb76fSMark Brown map->format.format_val(base + (map->cache_word_size * idx), 531eb4cb76fSMark Brown val, 0); 532eb4cb76fSMark Brown return false; 533eb4cb76fSMark Brown } 534eb4cb76fSMark Brown 535879082c9SMark Brown switch (map->cache_word_size) { 5369fabe24eSDimitris Papastamos case 1: { 5379fabe24eSDimitris Papastamos u8 *cache = base; 5389fabe24eSDimitris Papastamos cache[idx] = val; 5399fabe24eSDimitris Papastamos break; 5409fabe24eSDimitris Papastamos } 5419fabe24eSDimitris Papastamos case 2: { 5429fabe24eSDimitris Papastamos u16 *cache = base; 5439fabe24eSDimitris Papastamos cache[idx] = val; 5449fabe24eSDimitris Papastamos break; 5459fabe24eSDimitris Papastamos } 5467d5e525bSMark Brown case 4: { 5477d5e525bSMark Brown u32 *cache = base; 5487d5e525bSMark Brown cache[idx] = val; 5497d5e525bSMark Brown break; 5507d5e525bSMark Brown } 5519fabe24eSDimitris Papastamos default: 5529fabe24eSDimitris Papastamos BUG(); 5539fabe24eSDimitris Papastamos } 5549fabe24eSDimitris Papastamos return false; 5559fabe24eSDimitris Papastamos } 5569fabe24eSDimitris Papastamos 557879082c9SMark Brown unsigned int regcache_get_val(struct regmap *map, const void *base, 558879082c9SMark Brown unsigned int idx) 5599fabe24eSDimitris Papastamos { 5609fabe24eSDimitris Papastamos if (!base) 5619fabe24eSDimitris Papastamos return -EINVAL; 5629fabe24eSDimitris Papastamos 563eb4cb76fSMark Brown /* Use device native format if possible */ 564eb4cb76fSMark Brown if (map->format.parse_val) 5658817796bSMark Brown return map->format.parse_val(regcache_get_val_addr(map, base, 5668817796bSMark Brown idx)); 567eb4cb76fSMark Brown 568879082c9SMark Brown switch (map->cache_word_size) { 5699fabe24eSDimitris Papastamos case 1: { 5709fabe24eSDimitris Papastamos const u8 *cache = base; 5719fabe24eSDimitris Papastamos return cache[idx]; 5729fabe24eSDimitris Papastamos } 5739fabe24eSDimitris Papastamos case 2: { 5749fabe24eSDimitris Papastamos const u16 *cache = base; 5759fabe24eSDimitris Papastamos return cache[idx]; 5769fabe24eSDimitris Papastamos } 5777d5e525bSMark Brown case 4: { 5787d5e525bSMark Brown const u32 *cache = base; 5797d5e525bSMark Brown return cache[idx]; 5807d5e525bSMark Brown } 5819fabe24eSDimitris Papastamos default: 5829fabe24eSDimitris Papastamos BUG(); 5839fabe24eSDimitris Papastamos } 5849fabe24eSDimitris Papastamos /* unreachable */ 5859fabe24eSDimitris Papastamos return -1; 5869fabe24eSDimitris Papastamos } 5879fabe24eSDimitris Papastamos 588f094fea6SMark Brown static int regcache_default_cmp(const void *a, const void *b) 589c08604b8SDimitris Papastamos { 590c08604b8SDimitris Papastamos const struct reg_default *_a = a; 591c08604b8SDimitris Papastamos const struct reg_default *_b = b; 592c08604b8SDimitris Papastamos 593c08604b8SDimitris Papastamos return _a->reg - _b->reg; 594c08604b8SDimitris Papastamos } 595c08604b8SDimitris Papastamos 596f094fea6SMark Brown int regcache_lookup_reg(struct regmap *map, unsigned int reg) 597f094fea6SMark Brown { 598f094fea6SMark Brown struct reg_default key; 599f094fea6SMark Brown struct reg_default *r; 600f094fea6SMark Brown 601f094fea6SMark Brown key.reg = reg; 602f094fea6SMark Brown key.def = 0; 603f094fea6SMark Brown 604f094fea6SMark Brown r = bsearch(&key, map->reg_defaults, map->num_reg_defaults, 605f094fea6SMark Brown sizeof(struct reg_default), regcache_default_cmp); 606f094fea6SMark Brown 607f094fea6SMark Brown if (r) 608f094fea6SMark Brown return r - map->reg_defaults; 609f094fea6SMark Brown else 6106e6ace00SMark Brown return -ENOENT; 611f094fea6SMark Brown } 612f8bd822cSMark Brown 6133f4ff561SLars-Peter Clausen static bool regcache_reg_present(unsigned long *cache_present, unsigned int idx) 6143f4ff561SLars-Peter Clausen { 6153f4ff561SLars-Peter Clausen if (!cache_present) 6163f4ff561SLars-Peter Clausen return true; 6173f4ff561SLars-Peter Clausen 6183f4ff561SLars-Peter Clausen return test_bit(idx, cache_present); 6193f4ff561SLars-Peter Clausen } 6203f4ff561SLars-Peter Clausen 621cfdeb8c3SMark Brown static int regcache_sync_block_single(struct regmap *map, void *block, 6223f4ff561SLars-Peter Clausen unsigned long *cache_present, 623cfdeb8c3SMark Brown unsigned int block_base, 624cfdeb8c3SMark Brown unsigned int start, unsigned int end) 625cfdeb8c3SMark Brown { 626cfdeb8c3SMark Brown unsigned int i, regtmp, val; 627cfdeb8c3SMark Brown int ret; 628cfdeb8c3SMark Brown 629cfdeb8c3SMark Brown for (i = start; i < end; i++) { 630cfdeb8c3SMark Brown regtmp = block_base + (i * map->reg_stride); 631cfdeb8c3SMark Brown 6324ceba98dSTakashi Iwai if (!regcache_reg_present(cache_present, i) || 6334ceba98dSTakashi Iwai !regmap_writeable(map, regtmp)) 634cfdeb8c3SMark Brown continue; 635cfdeb8c3SMark Brown 636cfdeb8c3SMark Brown val = regcache_get_val(map, block, i); 6373969fa08SKevin Cernekee if (!regcache_reg_needs_sync(map, regtmp, val)) 638cfdeb8c3SMark Brown continue; 639cfdeb8c3SMark Brown 640cfdeb8c3SMark Brown map->cache_bypass = 1; 641cfdeb8c3SMark Brown 642cfdeb8c3SMark Brown ret = _regmap_write(map, regtmp, val); 643cfdeb8c3SMark Brown 644cfdeb8c3SMark Brown map->cache_bypass = 0; 645f29a4320SJarkko Nikula if (ret != 0) { 646f29a4320SJarkko Nikula dev_err(map->dev, "Unable to sync register %#x. %d\n", 647f29a4320SJarkko Nikula regtmp, ret); 648cfdeb8c3SMark Brown return ret; 649f29a4320SJarkko Nikula } 650cfdeb8c3SMark Brown dev_dbg(map->dev, "Synced register %#x, value %#x\n", 651cfdeb8c3SMark Brown regtmp, val); 652cfdeb8c3SMark Brown } 653cfdeb8c3SMark Brown 654cfdeb8c3SMark Brown return 0; 655cfdeb8c3SMark Brown } 656cfdeb8c3SMark Brown 65775a5f89fSMark Brown static int regcache_sync_block_raw_flush(struct regmap *map, const void **data, 65875a5f89fSMark Brown unsigned int base, unsigned int cur) 65975a5f89fSMark Brown { 66075a5f89fSMark Brown size_t val_bytes = map->format.val_bytes; 66175a5f89fSMark Brown int ret, count; 66275a5f89fSMark Brown 66375a5f89fSMark Brown if (*data == NULL) 66475a5f89fSMark Brown return 0; 66575a5f89fSMark Brown 66678ba73eeSDylan Reid count = (cur - base) / map->reg_stride; 66775a5f89fSMark Brown 6689659293cSStratos Karafotis dev_dbg(map->dev, "Writing %zu bytes for %d registers from 0x%x-0x%x\n", 66978ba73eeSDylan Reid count * val_bytes, count, base, cur - map->reg_stride); 67075a5f89fSMark Brown 67175a5f89fSMark Brown map->cache_bypass = 1; 67275a5f89fSMark Brown 6730a819809SMark Brown ret = _regmap_raw_write(map, base, *data, count * val_bytes); 674f29a4320SJarkko Nikula if (ret) 675f29a4320SJarkko Nikula dev_err(map->dev, "Unable to sync registers %#x-%#x. %d\n", 676f29a4320SJarkko Nikula base, cur - map->reg_stride, ret); 67775a5f89fSMark Brown 67875a5f89fSMark Brown map->cache_bypass = 0; 67975a5f89fSMark Brown 68075a5f89fSMark Brown *data = NULL; 68175a5f89fSMark Brown 68275a5f89fSMark Brown return ret; 68375a5f89fSMark Brown } 68475a5f89fSMark Brown 685f52687afSSachin Kamat static int regcache_sync_block_raw(struct regmap *map, void *block, 6863f4ff561SLars-Peter Clausen unsigned long *cache_present, 687f8bd822cSMark Brown unsigned int block_base, unsigned int start, 688f8bd822cSMark Brown unsigned int end) 689f8bd822cSMark Brown { 69075a5f89fSMark Brown unsigned int i, val; 69175a5f89fSMark Brown unsigned int regtmp = 0; 69275a5f89fSMark Brown unsigned int base = 0; 69375a5f89fSMark Brown const void *data = NULL; 694f8bd822cSMark Brown int ret; 695f8bd822cSMark Brown 696f8bd822cSMark Brown for (i = start; i < end; i++) { 697f8bd822cSMark Brown regtmp = block_base + (i * map->reg_stride); 698f8bd822cSMark Brown 6994ceba98dSTakashi Iwai if (!regcache_reg_present(cache_present, i) || 7004ceba98dSTakashi Iwai !regmap_writeable(map, regtmp)) { 70175a5f89fSMark Brown ret = regcache_sync_block_raw_flush(map, &data, 70275a5f89fSMark Brown base, regtmp); 70375a5f89fSMark Brown if (ret != 0) 70475a5f89fSMark Brown return ret; 705f8bd822cSMark Brown continue; 70675a5f89fSMark Brown } 707f8bd822cSMark Brown 708f8bd822cSMark Brown val = regcache_get_val(map, block, i); 7093969fa08SKevin Cernekee if (!regcache_reg_needs_sync(map, regtmp, val)) { 71075a5f89fSMark Brown ret = regcache_sync_block_raw_flush(map, &data, 71175a5f89fSMark Brown base, regtmp); 712f8bd822cSMark Brown if (ret != 0) 713f8bd822cSMark Brown return ret; 71475a5f89fSMark Brown continue; 715f8bd822cSMark Brown } 716f8bd822cSMark Brown 71775a5f89fSMark Brown if (!data) { 71875a5f89fSMark Brown data = regcache_get_val_addr(map, block, i); 71975a5f89fSMark Brown base = regtmp; 72075a5f89fSMark Brown } 72175a5f89fSMark Brown } 72275a5f89fSMark Brown 7232d49b598SLars-Peter Clausen return regcache_sync_block_raw_flush(map, &data, base, regtmp + 7242d49b598SLars-Peter Clausen map->reg_stride); 725f8bd822cSMark Brown } 726cfdeb8c3SMark Brown 727cfdeb8c3SMark Brown int regcache_sync_block(struct regmap *map, void *block, 7283f4ff561SLars-Peter Clausen unsigned long *cache_present, 729cfdeb8c3SMark Brown unsigned int block_base, unsigned int start, 730cfdeb8c3SMark Brown unsigned int end) 731cfdeb8c3SMark Brown { 7325c1ebe7fSMark Brown if (regmap_can_raw_write(map) && !map->use_single_rw) 7333f4ff561SLars-Peter Clausen return regcache_sync_block_raw(map, block, cache_present, 7343f4ff561SLars-Peter Clausen block_base, start, end); 735cfdeb8c3SMark Brown else 7363f4ff561SLars-Peter Clausen return regcache_sync_block_single(map, block, cache_present, 7373f4ff561SLars-Peter Clausen block_base, start, end); 738cfdeb8c3SMark Brown } 739