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) { 57621a5f7aSViresh Kumar bool 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*/ 61621a5f7aSViresh Kumar map->cache_bypass = true; 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 103e7a6db30SMark Brown if (map->cache_type == REGCACHE_NONE) { 1048cfe2fd3SXiubo Li if (config->reg_defaults || config->num_reg_defaults_raw) 1058cfe2fd3SXiubo Li dev_warn(map->dev, 1068cfe2fd3SXiubo Li "No cache used with register defaults set!\n"); 1078cfe2fd3SXiubo Li 108e7a6db30SMark Brown map->cache_bypass = true; 1099fabe24eSDimitris Papastamos return 0; 110e7a6db30SMark Brown } 1119fabe24eSDimitris Papastamos 112*167f7066SXiubo Li if (config->reg_defaults && !config->num_reg_defaults) { 113*167f7066SXiubo Li dev_err(map->dev, 114*167f7066SXiubo Li "Register defaults are set without the number!\n"); 115*167f7066SXiubo Li return -EINVAL; 116*167f7066SXiubo Li } 117*167f7066SXiubo Li 1188cfe2fd3SXiubo Li for (i = 0; i < config->num_reg_defaults; i++) 1198cfe2fd3SXiubo Li if (config->reg_defaults[i].reg % map->reg_stride) 1208cfe2fd3SXiubo Li return -EINVAL; 1218cfe2fd3SXiubo Li 1229fabe24eSDimitris Papastamos for (i = 0; i < ARRAY_SIZE(cache_types); i++) 1239fabe24eSDimitris Papastamos if (cache_types[i]->type == map->cache_type) 1249fabe24eSDimitris Papastamos break; 1259fabe24eSDimitris Papastamos 1269fabe24eSDimitris Papastamos if (i == ARRAY_SIZE(cache_types)) { 1279fabe24eSDimitris Papastamos dev_err(map->dev, "Could not match compress type: %d\n", 1289fabe24eSDimitris Papastamos map->cache_type); 1299fabe24eSDimitris Papastamos return -EINVAL; 1309fabe24eSDimitris Papastamos } 1319fabe24eSDimitris Papastamos 132e5e3b8abSLars-Peter Clausen map->num_reg_defaults = config->num_reg_defaults; 133e5e3b8abSLars-Peter Clausen map->num_reg_defaults_raw = config->num_reg_defaults_raw; 134e5e3b8abSLars-Peter Clausen map->reg_defaults_raw = config->reg_defaults_raw; 135064d4db1SLars-Peter Clausen map->cache_word_size = DIV_ROUND_UP(config->val_bits, 8); 136064d4db1SLars-Peter Clausen map->cache_size_raw = map->cache_word_size * config->num_reg_defaults_raw; 137e5e3b8abSLars-Peter Clausen 1389fabe24eSDimitris Papastamos map->cache = NULL; 1399fabe24eSDimitris Papastamos map->cache_ops = cache_types[i]; 1409fabe24eSDimitris Papastamos 1419fabe24eSDimitris Papastamos if (!map->cache_ops->read || 1429fabe24eSDimitris Papastamos !map->cache_ops->write || 1439fabe24eSDimitris Papastamos !map->cache_ops->name) 1449fabe24eSDimitris Papastamos return -EINVAL; 1459fabe24eSDimitris Papastamos 1469fabe24eSDimitris Papastamos /* We still need to ensure that the reg_defaults 1479fabe24eSDimitris Papastamos * won't vanish from under us. We'll need to make 1489fabe24eSDimitris Papastamos * a copy of it. 1499fabe24eSDimitris Papastamos */ 150720e4616SLars-Peter Clausen if (config->reg_defaults) { 151720e4616SLars-Peter Clausen tmp_buf = kmemdup(config->reg_defaults, map->num_reg_defaults * 1529fabe24eSDimitris Papastamos sizeof(struct reg_default), GFP_KERNEL); 1539fabe24eSDimitris Papastamos if (!tmp_buf) 1549fabe24eSDimitris Papastamos return -ENOMEM; 1559fabe24eSDimitris Papastamos map->reg_defaults = tmp_buf; 1568528bdd4SMark Brown } else if (map->num_reg_defaults_raw) { 1575fcd2560SMark Brown /* Some devices such as PMICs don't have cache defaults, 1589fabe24eSDimitris Papastamos * we cope with this by reading back the HW registers and 1599fabe24eSDimitris Papastamos * crafting the cache defaults by hand. 1609fabe24eSDimitris Papastamos */ 1619fabe24eSDimitris Papastamos ret = regcache_hw_init(map); 1629fabe24eSDimitris Papastamos if (ret < 0) 1639fabe24eSDimitris Papastamos return ret; 164fb70067eSXiubo Li if (map->cache_bypass) 165fb70067eSXiubo Li return 0; 1669fabe24eSDimitris Papastamos } 1679fabe24eSDimitris Papastamos 1689fabe24eSDimitris Papastamos if (!map->max_register) 1699fabe24eSDimitris Papastamos map->max_register = map->num_reg_defaults_raw; 1709fabe24eSDimitris Papastamos 1719fabe24eSDimitris Papastamos if (map->cache_ops->init) { 1729fabe24eSDimitris Papastamos dev_dbg(map->dev, "Initializing %s cache\n", 1739fabe24eSDimitris Papastamos map->cache_ops->name); 174bd061c78SLars-Peter Clausen ret = map->cache_ops->init(map); 175bd061c78SLars-Peter Clausen if (ret) 176bd061c78SLars-Peter Clausen goto err_free; 1779fabe24eSDimitris Papastamos } 1789fabe24eSDimitris Papastamos return 0; 179bd061c78SLars-Peter Clausen 180bd061c78SLars-Peter Clausen err_free: 181bd061c78SLars-Peter Clausen kfree(map->reg_defaults); 182bd061c78SLars-Peter Clausen if (map->cache_free) 183bd061c78SLars-Peter Clausen kfree(map->reg_defaults_raw); 184bd061c78SLars-Peter Clausen 185bd061c78SLars-Peter Clausen return ret; 1869fabe24eSDimitris Papastamos } 1879fabe24eSDimitris Papastamos 1889fabe24eSDimitris Papastamos void regcache_exit(struct regmap *map) 1899fabe24eSDimitris Papastamos { 1909fabe24eSDimitris Papastamos if (map->cache_type == REGCACHE_NONE) 1919fabe24eSDimitris Papastamos return; 1929fabe24eSDimitris Papastamos 1939fabe24eSDimitris Papastamos BUG_ON(!map->cache_ops); 1949fabe24eSDimitris Papastamos 1959fabe24eSDimitris Papastamos kfree(map->reg_defaults); 1969fabe24eSDimitris Papastamos if (map->cache_free) 1979fabe24eSDimitris Papastamos kfree(map->reg_defaults_raw); 1989fabe24eSDimitris Papastamos 1999fabe24eSDimitris Papastamos if (map->cache_ops->exit) { 2009fabe24eSDimitris Papastamos dev_dbg(map->dev, "Destroying %s cache\n", 2019fabe24eSDimitris Papastamos map->cache_ops->name); 2029fabe24eSDimitris Papastamos map->cache_ops->exit(map); 2039fabe24eSDimitris Papastamos } 2049fabe24eSDimitris Papastamos } 2059fabe24eSDimitris Papastamos 2069fabe24eSDimitris Papastamos /** 2079fabe24eSDimitris Papastamos * regcache_read: Fetch the value of a given register from the cache. 2089fabe24eSDimitris Papastamos * 2099fabe24eSDimitris Papastamos * @map: map to configure. 2109fabe24eSDimitris Papastamos * @reg: The register index. 2119fabe24eSDimitris Papastamos * @value: The value to be returned. 2129fabe24eSDimitris Papastamos * 2139fabe24eSDimitris Papastamos * Return a negative value on failure, 0 on success. 2149fabe24eSDimitris Papastamos */ 2159fabe24eSDimitris Papastamos int regcache_read(struct regmap *map, 2169fabe24eSDimitris Papastamos unsigned int reg, unsigned int *value) 2179fabe24eSDimitris Papastamos { 218bc7ee556SMark Brown int ret; 219bc7ee556SMark Brown 2209fabe24eSDimitris Papastamos if (map->cache_type == REGCACHE_NONE) 2219fabe24eSDimitris Papastamos return -ENOSYS; 2229fabe24eSDimitris Papastamos 2239fabe24eSDimitris Papastamos BUG_ON(!map->cache_ops); 2249fabe24eSDimitris Papastamos 225bc7ee556SMark Brown if (!regmap_volatile(map, reg)) { 226bc7ee556SMark Brown ret = map->cache_ops->read(map, reg, value); 227bc7ee556SMark Brown 228bc7ee556SMark Brown if (ret == 0) 229c6b570d9SPhilipp Zabel trace_regmap_reg_read_cache(map, reg, *value); 230bc7ee556SMark Brown 231bc7ee556SMark Brown return ret; 232bc7ee556SMark Brown } 2339fabe24eSDimitris Papastamos 2349fabe24eSDimitris Papastamos return -EINVAL; 2359fabe24eSDimitris Papastamos } 2369fabe24eSDimitris Papastamos 2379fabe24eSDimitris Papastamos /** 2389fabe24eSDimitris Papastamos * regcache_write: Set the value of a given register in the cache. 2399fabe24eSDimitris Papastamos * 2409fabe24eSDimitris Papastamos * @map: map to configure. 2419fabe24eSDimitris Papastamos * @reg: The register index. 2429fabe24eSDimitris Papastamos * @value: The new register value. 2439fabe24eSDimitris Papastamos * 2449fabe24eSDimitris Papastamos * Return a negative value on failure, 0 on success. 2459fabe24eSDimitris Papastamos */ 2469fabe24eSDimitris Papastamos int regcache_write(struct regmap *map, 2479fabe24eSDimitris Papastamos unsigned int reg, unsigned int value) 2489fabe24eSDimitris Papastamos { 2499fabe24eSDimitris Papastamos if (map->cache_type == REGCACHE_NONE) 2509fabe24eSDimitris Papastamos return 0; 2519fabe24eSDimitris Papastamos 2529fabe24eSDimitris Papastamos BUG_ON(!map->cache_ops); 2539fabe24eSDimitris Papastamos 2549fabe24eSDimitris Papastamos if (!regmap_volatile(map, reg)) 2559fabe24eSDimitris Papastamos return map->cache_ops->write(map, reg, value); 2569fabe24eSDimitris Papastamos 2579fabe24eSDimitris Papastamos return 0; 2589fabe24eSDimitris Papastamos } 2599fabe24eSDimitris Papastamos 2603969fa08SKevin Cernekee static bool regcache_reg_needs_sync(struct regmap *map, unsigned int reg, 2613969fa08SKevin Cernekee unsigned int val) 2623969fa08SKevin Cernekee { 2633969fa08SKevin Cernekee int ret; 2643969fa08SKevin Cernekee 2651c79771aSKevin Cernekee /* If we don't know the chip just got reset, then sync everything. */ 2661c79771aSKevin Cernekee if (!map->no_sync_defaults) 2671c79771aSKevin Cernekee return true; 2681c79771aSKevin Cernekee 2693969fa08SKevin Cernekee /* Is this the hardware default? If so skip. */ 2703969fa08SKevin Cernekee ret = regcache_lookup_reg(map, reg); 2713969fa08SKevin Cernekee if (ret >= 0 && val == map->reg_defaults[ret].def) 2723969fa08SKevin Cernekee return false; 2733969fa08SKevin Cernekee return true; 2743969fa08SKevin Cernekee } 2753969fa08SKevin Cernekee 276d856fce4SMaarten ter Huurne static int regcache_default_sync(struct regmap *map, unsigned int min, 277d856fce4SMaarten ter Huurne unsigned int max) 278d856fce4SMaarten ter Huurne { 279d856fce4SMaarten ter Huurne unsigned int reg; 280d856fce4SMaarten ter Huurne 28175617328SDylan Reid for (reg = min; reg <= max; reg += map->reg_stride) { 282d856fce4SMaarten ter Huurne unsigned int val; 283d856fce4SMaarten ter Huurne int ret; 284d856fce4SMaarten ter Huurne 28583f8475cSDylan Reid if (regmap_volatile(map, reg) || 28683f8475cSDylan Reid !regmap_writeable(map, reg)) 287d856fce4SMaarten ter Huurne continue; 288d856fce4SMaarten ter Huurne 289d856fce4SMaarten ter Huurne ret = regcache_read(map, reg, &val); 290d856fce4SMaarten ter Huurne if (ret) 291d856fce4SMaarten ter Huurne return ret; 292d856fce4SMaarten ter Huurne 2933969fa08SKevin Cernekee if (!regcache_reg_needs_sync(map, reg, val)) 294d856fce4SMaarten ter Huurne continue; 295d856fce4SMaarten ter Huurne 296621a5f7aSViresh Kumar map->cache_bypass = true; 297d856fce4SMaarten ter Huurne ret = _regmap_write(map, reg, val); 298621a5f7aSViresh Kumar map->cache_bypass = false; 299f29a4320SJarkko Nikula if (ret) { 300f29a4320SJarkko Nikula dev_err(map->dev, "Unable to sync register %#x. %d\n", 301f29a4320SJarkko Nikula reg, ret); 302d856fce4SMaarten ter Huurne return ret; 303f29a4320SJarkko Nikula } 304d856fce4SMaarten ter Huurne dev_dbg(map->dev, "Synced register %#x, value %#x\n", reg, val); 305d856fce4SMaarten ter Huurne } 306d856fce4SMaarten ter Huurne 307d856fce4SMaarten ter Huurne return 0; 308d856fce4SMaarten ter Huurne } 309d856fce4SMaarten ter Huurne 3109fabe24eSDimitris Papastamos /** 3119fabe24eSDimitris Papastamos * regcache_sync: Sync the register cache with the hardware. 3129fabe24eSDimitris Papastamos * 3139fabe24eSDimitris Papastamos * @map: map to configure. 3149fabe24eSDimitris Papastamos * 3159fabe24eSDimitris Papastamos * Any registers that should not be synced should be marked as 3169fabe24eSDimitris Papastamos * volatile. In general drivers can choose not to use the provided 3179fabe24eSDimitris Papastamos * syncing functionality if they so require. 3189fabe24eSDimitris Papastamos * 3199fabe24eSDimitris Papastamos * Return a negative value on failure, 0 on success. 3209fabe24eSDimitris Papastamos */ 3219fabe24eSDimitris Papastamos int regcache_sync(struct regmap *map) 3229fabe24eSDimitris Papastamos { 323954757d7SDimitris Papastamos int ret = 0; 324954757d7SDimitris Papastamos unsigned int i; 32559360089SDimitris Papastamos const char *name; 326621a5f7aSViresh Kumar bool bypass; 32759360089SDimitris Papastamos 328d856fce4SMaarten ter Huurne BUG_ON(!map->cache_ops); 3299fabe24eSDimitris Papastamos 33081485f52SLars-Peter Clausen map->lock(map->lock_arg); 331beb1a10fSDimitris Papastamos /* Remember the initial bypass state */ 332beb1a10fSDimitris Papastamos bypass = map->cache_bypass; 3339fabe24eSDimitris Papastamos dev_dbg(map->dev, "Syncing %s cache\n", 3349fabe24eSDimitris Papastamos map->cache_ops->name); 33559360089SDimitris Papastamos name = map->cache_ops->name; 336c6b570d9SPhilipp Zabel trace_regcache_sync(map, name, "start"); 33722f0d90aSMark Brown 3388ae0d7e8SMark Brown if (!map->cache_dirty) 3398ae0d7e8SMark Brown goto out; 340d9db7627SMark Brown 341affbe886SMark Brown map->async = true; 342affbe886SMark Brown 34322f0d90aSMark Brown /* Apply any patch first */ 344621a5f7aSViresh Kumar map->cache_bypass = true; 34522f0d90aSMark Brown for (i = 0; i < map->patch_regs; i++) { 34622f0d90aSMark Brown ret = _regmap_write(map, map->patch[i].reg, map->patch[i].def); 34722f0d90aSMark Brown if (ret != 0) { 34822f0d90aSMark Brown dev_err(map->dev, "Failed to write %x = %x: %d\n", 34922f0d90aSMark Brown map->patch[i].reg, map->patch[i].def, ret); 35022f0d90aSMark Brown goto out; 35122f0d90aSMark Brown } 35222f0d90aSMark Brown } 353621a5f7aSViresh Kumar map->cache_bypass = false; 35422f0d90aSMark Brown 355d856fce4SMaarten ter Huurne if (map->cache_ops->sync) 356ac8d91c8SMark Brown ret = map->cache_ops->sync(map, 0, map->max_register); 357d856fce4SMaarten ter Huurne else 358d856fce4SMaarten ter Huurne ret = regcache_default_sync(map, 0, map->max_register); 359954757d7SDimitris Papastamos 3606ff73738SMark Brown if (ret == 0) 3616ff73738SMark Brown map->cache_dirty = false; 3626ff73738SMark Brown 363954757d7SDimitris Papastamos out: 364beb1a10fSDimitris Papastamos /* Restore the bypass state */ 365affbe886SMark Brown map->async = false; 366beb1a10fSDimitris Papastamos map->cache_bypass = bypass; 3671c79771aSKevin Cernekee map->no_sync_defaults = false; 36881485f52SLars-Peter Clausen map->unlock(map->lock_arg); 369954757d7SDimitris Papastamos 370affbe886SMark Brown regmap_async_complete(map); 371affbe886SMark Brown 372c6b570d9SPhilipp Zabel trace_regcache_sync(map, name, "stop"); 373affbe886SMark Brown 374954757d7SDimitris Papastamos return ret; 3759fabe24eSDimitris Papastamos } 3769fabe24eSDimitris Papastamos EXPORT_SYMBOL_GPL(regcache_sync); 3779fabe24eSDimitris Papastamos 37892afb286SMark Brown /** 3794d4cfd16SMark Brown * regcache_sync_region: Sync part of the register cache with the hardware. 3804d4cfd16SMark Brown * 3814d4cfd16SMark Brown * @map: map to sync. 3824d4cfd16SMark Brown * @min: first register to sync 3834d4cfd16SMark Brown * @max: last register to sync 3844d4cfd16SMark Brown * 3854d4cfd16SMark Brown * Write all non-default register values in the specified region to 3864d4cfd16SMark Brown * the hardware. 3874d4cfd16SMark Brown * 3884d4cfd16SMark Brown * Return a negative value on failure, 0 on success. 3894d4cfd16SMark Brown */ 3904d4cfd16SMark Brown int regcache_sync_region(struct regmap *map, unsigned int min, 3914d4cfd16SMark Brown unsigned int max) 3924d4cfd16SMark Brown { 3934d4cfd16SMark Brown int ret = 0; 3944d4cfd16SMark Brown const char *name; 395621a5f7aSViresh Kumar bool bypass; 3964d4cfd16SMark Brown 397d856fce4SMaarten ter Huurne BUG_ON(!map->cache_ops); 3984d4cfd16SMark Brown 39981485f52SLars-Peter Clausen map->lock(map->lock_arg); 4004d4cfd16SMark Brown 4014d4cfd16SMark Brown /* Remember the initial bypass state */ 4024d4cfd16SMark Brown bypass = map->cache_bypass; 4034d4cfd16SMark Brown 4044d4cfd16SMark Brown name = map->cache_ops->name; 4054d4cfd16SMark Brown dev_dbg(map->dev, "Syncing %s cache from %d-%d\n", name, min, max); 4064d4cfd16SMark Brown 407c6b570d9SPhilipp Zabel trace_regcache_sync(map, name, "start region"); 4084d4cfd16SMark Brown 4094d4cfd16SMark Brown if (!map->cache_dirty) 4104d4cfd16SMark Brown goto out; 4114d4cfd16SMark Brown 412affbe886SMark Brown map->async = true; 413affbe886SMark Brown 414d856fce4SMaarten ter Huurne if (map->cache_ops->sync) 4154d4cfd16SMark Brown ret = map->cache_ops->sync(map, min, max); 416d856fce4SMaarten ter Huurne else 417d856fce4SMaarten ter Huurne ret = regcache_default_sync(map, min, max); 4184d4cfd16SMark Brown 4194d4cfd16SMark Brown out: 4204d4cfd16SMark Brown /* Restore the bypass state */ 4214d4cfd16SMark Brown map->cache_bypass = bypass; 422affbe886SMark Brown map->async = false; 4231c79771aSKevin Cernekee map->no_sync_defaults = false; 42481485f52SLars-Peter Clausen map->unlock(map->lock_arg); 4254d4cfd16SMark Brown 426affbe886SMark Brown regmap_async_complete(map); 427affbe886SMark Brown 428c6b570d9SPhilipp Zabel trace_regcache_sync(map, name, "stop region"); 429affbe886SMark Brown 4304d4cfd16SMark Brown return ret; 4314d4cfd16SMark Brown } 432e466de05SMark Brown EXPORT_SYMBOL_GPL(regcache_sync_region); 4334d4cfd16SMark Brown 4344d4cfd16SMark Brown /** 435697e85bcSMark Brown * regcache_drop_region: Discard part of the register cache 436697e85bcSMark Brown * 437697e85bcSMark Brown * @map: map to operate on 438697e85bcSMark Brown * @min: first register to discard 439697e85bcSMark Brown * @max: last register to discard 440697e85bcSMark Brown * 441697e85bcSMark Brown * Discard part of the register cache. 442697e85bcSMark Brown * 443697e85bcSMark Brown * Return a negative value on failure, 0 on success. 444697e85bcSMark Brown */ 445697e85bcSMark Brown int regcache_drop_region(struct regmap *map, unsigned int min, 446697e85bcSMark Brown unsigned int max) 447697e85bcSMark Brown { 448697e85bcSMark Brown int ret = 0; 449697e85bcSMark Brown 4503f4ff561SLars-Peter Clausen if (!map->cache_ops || !map->cache_ops->drop) 451697e85bcSMark Brown return -EINVAL; 452697e85bcSMark Brown 45381485f52SLars-Peter Clausen map->lock(map->lock_arg); 454697e85bcSMark Brown 455c6b570d9SPhilipp Zabel trace_regcache_drop_region(map, min, max); 456697e85bcSMark Brown 457697e85bcSMark Brown ret = map->cache_ops->drop(map, min, max); 458697e85bcSMark Brown 45981485f52SLars-Peter Clausen map->unlock(map->lock_arg); 460697e85bcSMark Brown 461697e85bcSMark Brown return ret; 462697e85bcSMark Brown } 463697e85bcSMark Brown EXPORT_SYMBOL_GPL(regcache_drop_region); 464697e85bcSMark Brown 465697e85bcSMark Brown /** 46692afb286SMark Brown * regcache_cache_only: Put a register map into cache only mode 46792afb286SMark Brown * 46892afb286SMark Brown * @map: map to configure 46992afb286SMark Brown * @cache_only: flag if changes should be written to the hardware 47092afb286SMark Brown * 47192afb286SMark Brown * When a register map is marked as cache only writes to the register 47292afb286SMark Brown * map API will only update the register cache, they will not cause 47392afb286SMark Brown * any hardware changes. This is useful for allowing portions of 47492afb286SMark Brown * drivers to act as though the device were functioning as normal when 47592afb286SMark Brown * it is disabled for power saving reasons. 47692afb286SMark Brown */ 47792afb286SMark Brown void regcache_cache_only(struct regmap *map, bool enable) 47892afb286SMark Brown { 47981485f52SLars-Peter Clausen map->lock(map->lock_arg); 480ac77a765SDimitris Papastamos WARN_ON(map->cache_bypass && enable); 48192afb286SMark Brown map->cache_only = enable; 482c6b570d9SPhilipp Zabel trace_regmap_cache_only(map, enable); 48381485f52SLars-Peter Clausen map->unlock(map->lock_arg); 48492afb286SMark Brown } 48592afb286SMark Brown EXPORT_SYMBOL_GPL(regcache_cache_only); 48692afb286SMark Brown 4876eb0f5e0SDimitris Papastamos /** 4881c79771aSKevin Cernekee * regcache_mark_dirty: Indicate that HW registers were reset to default values 4898ae0d7e8SMark Brown * 4908ae0d7e8SMark Brown * @map: map to mark 4918ae0d7e8SMark Brown * 4921c79771aSKevin Cernekee * Inform regcache that the device has been powered down or reset, so that 4931c79771aSKevin Cernekee * on resume, regcache_sync() knows to write out all non-default values 4941c79771aSKevin Cernekee * stored in the cache. 4951c79771aSKevin Cernekee * 4961c79771aSKevin Cernekee * If this function is not called, regcache_sync() will assume that 4971c79771aSKevin Cernekee * the hardware state still matches the cache state, modulo any writes that 4981c79771aSKevin Cernekee * happened when cache_only was true. 4998ae0d7e8SMark Brown */ 5008ae0d7e8SMark Brown void regcache_mark_dirty(struct regmap *map) 5018ae0d7e8SMark Brown { 50281485f52SLars-Peter Clausen map->lock(map->lock_arg); 5038ae0d7e8SMark Brown map->cache_dirty = true; 5041c79771aSKevin Cernekee map->no_sync_defaults = true; 50581485f52SLars-Peter Clausen map->unlock(map->lock_arg); 5068ae0d7e8SMark Brown } 5078ae0d7e8SMark Brown EXPORT_SYMBOL_GPL(regcache_mark_dirty); 5088ae0d7e8SMark Brown 5098ae0d7e8SMark Brown /** 5106eb0f5e0SDimitris Papastamos * regcache_cache_bypass: Put a register map into cache bypass mode 5116eb0f5e0SDimitris Papastamos * 5126eb0f5e0SDimitris Papastamos * @map: map to configure 5130eef6b04SDimitris Papastamos * @cache_bypass: flag if changes should not be written to the hardware 5146eb0f5e0SDimitris Papastamos * 5156eb0f5e0SDimitris Papastamos * When a register map is marked with the cache bypass option, writes 5166eb0f5e0SDimitris Papastamos * to the register map API will only update the hardware and not the 5176eb0f5e0SDimitris Papastamos * the cache directly. This is useful when syncing the cache back to 5186eb0f5e0SDimitris Papastamos * the hardware. 5196eb0f5e0SDimitris Papastamos */ 5206eb0f5e0SDimitris Papastamos void regcache_cache_bypass(struct regmap *map, bool enable) 5216eb0f5e0SDimitris Papastamos { 52281485f52SLars-Peter Clausen map->lock(map->lock_arg); 523ac77a765SDimitris Papastamos WARN_ON(map->cache_only && enable); 5246eb0f5e0SDimitris Papastamos map->cache_bypass = enable; 525c6b570d9SPhilipp Zabel trace_regmap_cache_bypass(map, enable); 52681485f52SLars-Peter Clausen map->unlock(map->lock_arg); 5276eb0f5e0SDimitris Papastamos } 5286eb0f5e0SDimitris Papastamos EXPORT_SYMBOL_GPL(regcache_cache_bypass); 5296eb0f5e0SDimitris Papastamos 530879082c9SMark Brown bool regcache_set_val(struct regmap *map, void *base, unsigned int idx, 531879082c9SMark Brown unsigned int val) 5329fabe24eSDimitris Papastamos { 533325acab4SMark Brown if (regcache_get_val(map, base, idx) == val) 534325acab4SMark Brown return true; 535325acab4SMark Brown 536eb4cb76fSMark Brown /* Use device native format if possible */ 537eb4cb76fSMark Brown if (map->format.format_val) { 538eb4cb76fSMark Brown map->format.format_val(base + (map->cache_word_size * idx), 539eb4cb76fSMark Brown val, 0); 540eb4cb76fSMark Brown return false; 541eb4cb76fSMark Brown } 542eb4cb76fSMark Brown 543879082c9SMark Brown switch (map->cache_word_size) { 5449fabe24eSDimitris Papastamos case 1: { 5459fabe24eSDimitris Papastamos u8 *cache = base; 5469fabe24eSDimitris Papastamos cache[idx] = val; 5479fabe24eSDimitris Papastamos break; 5489fabe24eSDimitris Papastamos } 5499fabe24eSDimitris Papastamos case 2: { 5509fabe24eSDimitris Papastamos u16 *cache = base; 5519fabe24eSDimitris Papastamos cache[idx] = val; 5529fabe24eSDimitris Papastamos break; 5539fabe24eSDimitris Papastamos } 5547d5e525bSMark Brown case 4: { 5557d5e525bSMark Brown u32 *cache = base; 5567d5e525bSMark Brown cache[idx] = val; 5577d5e525bSMark Brown break; 5587d5e525bSMark Brown } 5599fabe24eSDimitris Papastamos default: 5609fabe24eSDimitris Papastamos BUG(); 5619fabe24eSDimitris Papastamos } 5629fabe24eSDimitris Papastamos return false; 5639fabe24eSDimitris Papastamos } 5649fabe24eSDimitris Papastamos 565879082c9SMark Brown unsigned int regcache_get_val(struct regmap *map, const void *base, 566879082c9SMark Brown unsigned int idx) 5679fabe24eSDimitris Papastamos { 5689fabe24eSDimitris Papastamos if (!base) 5699fabe24eSDimitris Papastamos return -EINVAL; 5709fabe24eSDimitris Papastamos 571eb4cb76fSMark Brown /* Use device native format if possible */ 572eb4cb76fSMark Brown if (map->format.parse_val) 5738817796bSMark Brown return map->format.parse_val(regcache_get_val_addr(map, base, 5748817796bSMark Brown idx)); 575eb4cb76fSMark Brown 576879082c9SMark Brown switch (map->cache_word_size) { 5779fabe24eSDimitris Papastamos case 1: { 5789fabe24eSDimitris Papastamos const u8 *cache = base; 5799fabe24eSDimitris Papastamos return cache[idx]; 5809fabe24eSDimitris Papastamos } 5819fabe24eSDimitris Papastamos case 2: { 5829fabe24eSDimitris Papastamos const u16 *cache = base; 5839fabe24eSDimitris Papastamos return cache[idx]; 5849fabe24eSDimitris Papastamos } 5857d5e525bSMark Brown case 4: { 5867d5e525bSMark Brown const u32 *cache = base; 5877d5e525bSMark Brown return cache[idx]; 5887d5e525bSMark Brown } 5899fabe24eSDimitris Papastamos default: 5909fabe24eSDimitris Papastamos BUG(); 5919fabe24eSDimitris Papastamos } 5929fabe24eSDimitris Papastamos /* unreachable */ 5939fabe24eSDimitris Papastamos return -1; 5949fabe24eSDimitris Papastamos } 5959fabe24eSDimitris Papastamos 596f094fea6SMark Brown static int regcache_default_cmp(const void *a, const void *b) 597c08604b8SDimitris Papastamos { 598c08604b8SDimitris Papastamos const struct reg_default *_a = a; 599c08604b8SDimitris Papastamos const struct reg_default *_b = b; 600c08604b8SDimitris Papastamos 601c08604b8SDimitris Papastamos return _a->reg - _b->reg; 602c08604b8SDimitris Papastamos } 603c08604b8SDimitris Papastamos 604f094fea6SMark Brown int regcache_lookup_reg(struct regmap *map, unsigned int reg) 605f094fea6SMark Brown { 606f094fea6SMark Brown struct reg_default key; 607f094fea6SMark Brown struct reg_default *r; 608f094fea6SMark Brown 609f094fea6SMark Brown key.reg = reg; 610f094fea6SMark Brown key.def = 0; 611f094fea6SMark Brown 612f094fea6SMark Brown r = bsearch(&key, map->reg_defaults, map->num_reg_defaults, 613f094fea6SMark Brown sizeof(struct reg_default), regcache_default_cmp); 614f094fea6SMark Brown 615f094fea6SMark Brown if (r) 616f094fea6SMark Brown return r - map->reg_defaults; 617f094fea6SMark Brown else 6186e6ace00SMark Brown return -ENOENT; 619f094fea6SMark Brown } 620f8bd822cSMark Brown 6213f4ff561SLars-Peter Clausen static bool regcache_reg_present(unsigned long *cache_present, unsigned int idx) 6223f4ff561SLars-Peter Clausen { 6233f4ff561SLars-Peter Clausen if (!cache_present) 6243f4ff561SLars-Peter Clausen return true; 6253f4ff561SLars-Peter Clausen 6263f4ff561SLars-Peter Clausen return test_bit(idx, cache_present); 6273f4ff561SLars-Peter Clausen } 6283f4ff561SLars-Peter Clausen 629cfdeb8c3SMark Brown static int regcache_sync_block_single(struct regmap *map, void *block, 6303f4ff561SLars-Peter Clausen unsigned long *cache_present, 631cfdeb8c3SMark Brown unsigned int block_base, 632cfdeb8c3SMark Brown unsigned int start, unsigned int end) 633cfdeb8c3SMark Brown { 634cfdeb8c3SMark Brown unsigned int i, regtmp, val; 635cfdeb8c3SMark Brown int ret; 636cfdeb8c3SMark Brown 637cfdeb8c3SMark Brown for (i = start; i < end; i++) { 638cfdeb8c3SMark Brown regtmp = block_base + (i * map->reg_stride); 639cfdeb8c3SMark Brown 6404ceba98dSTakashi Iwai if (!regcache_reg_present(cache_present, i) || 6414ceba98dSTakashi Iwai !regmap_writeable(map, regtmp)) 642cfdeb8c3SMark Brown continue; 643cfdeb8c3SMark Brown 644cfdeb8c3SMark Brown val = regcache_get_val(map, block, i); 6453969fa08SKevin Cernekee if (!regcache_reg_needs_sync(map, regtmp, val)) 646cfdeb8c3SMark Brown continue; 647cfdeb8c3SMark Brown 648621a5f7aSViresh Kumar map->cache_bypass = true; 649cfdeb8c3SMark Brown 650cfdeb8c3SMark Brown ret = _regmap_write(map, regtmp, val); 651cfdeb8c3SMark Brown 652621a5f7aSViresh Kumar map->cache_bypass = false; 653f29a4320SJarkko Nikula if (ret != 0) { 654f29a4320SJarkko Nikula dev_err(map->dev, "Unable to sync register %#x. %d\n", 655f29a4320SJarkko Nikula regtmp, ret); 656cfdeb8c3SMark Brown return ret; 657f29a4320SJarkko Nikula } 658cfdeb8c3SMark Brown dev_dbg(map->dev, "Synced register %#x, value %#x\n", 659cfdeb8c3SMark Brown regtmp, val); 660cfdeb8c3SMark Brown } 661cfdeb8c3SMark Brown 662cfdeb8c3SMark Brown return 0; 663cfdeb8c3SMark Brown } 664cfdeb8c3SMark Brown 66575a5f89fSMark Brown static int regcache_sync_block_raw_flush(struct regmap *map, const void **data, 66675a5f89fSMark Brown unsigned int base, unsigned int cur) 66775a5f89fSMark Brown { 66875a5f89fSMark Brown size_t val_bytes = map->format.val_bytes; 66975a5f89fSMark Brown int ret, count; 67075a5f89fSMark Brown 67175a5f89fSMark Brown if (*data == NULL) 67275a5f89fSMark Brown return 0; 67375a5f89fSMark Brown 67478ba73eeSDylan Reid count = (cur - base) / map->reg_stride; 67575a5f89fSMark Brown 6769659293cSStratos Karafotis dev_dbg(map->dev, "Writing %zu bytes for %d registers from 0x%x-0x%x\n", 67778ba73eeSDylan Reid count * val_bytes, count, base, cur - map->reg_stride); 67875a5f89fSMark Brown 679621a5f7aSViresh Kumar map->cache_bypass = true; 68075a5f89fSMark Brown 6810a819809SMark Brown ret = _regmap_raw_write(map, base, *data, count * val_bytes); 682f29a4320SJarkko Nikula if (ret) 683f29a4320SJarkko Nikula dev_err(map->dev, "Unable to sync registers %#x-%#x. %d\n", 684f29a4320SJarkko Nikula base, cur - map->reg_stride, ret); 68575a5f89fSMark Brown 686621a5f7aSViresh Kumar map->cache_bypass = false; 68775a5f89fSMark Brown 68875a5f89fSMark Brown *data = NULL; 68975a5f89fSMark Brown 69075a5f89fSMark Brown return ret; 69175a5f89fSMark Brown } 69275a5f89fSMark Brown 693f52687afSSachin Kamat static int regcache_sync_block_raw(struct regmap *map, void *block, 6943f4ff561SLars-Peter Clausen unsigned long *cache_present, 695f8bd822cSMark Brown unsigned int block_base, unsigned int start, 696f8bd822cSMark Brown unsigned int end) 697f8bd822cSMark Brown { 69875a5f89fSMark Brown unsigned int i, val; 69975a5f89fSMark Brown unsigned int regtmp = 0; 70075a5f89fSMark Brown unsigned int base = 0; 70175a5f89fSMark Brown const void *data = NULL; 702f8bd822cSMark Brown int ret; 703f8bd822cSMark Brown 704f8bd822cSMark Brown for (i = start; i < end; i++) { 705f8bd822cSMark Brown regtmp = block_base + (i * map->reg_stride); 706f8bd822cSMark Brown 7074ceba98dSTakashi Iwai if (!regcache_reg_present(cache_present, i) || 7084ceba98dSTakashi Iwai !regmap_writeable(map, regtmp)) { 70975a5f89fSMark Brown ret = regcache_sync_block_raw_flush(map, &data, 71075a5f89fSMark Brown base, regtmp); 71175a5f89fSMark Brown if (ret != 0) 71275a5f89fSMark Brown return ret; 713f8bd822cSMark Brown continue; 71475a5f89fSMark Brown } 715f8bd822cSMark Brown 716f8bd822cSMark Brown val = regcache_get_val(map, block, i); 7173969fa08SKevin Cernekee if (!regcache_reg_needs_sync(map, regtmp, val)) { 71875a5f89fSMark Brown ret = regcache_sync_block_raw_flush(map, &data, 71975a5f89fSMark Brown base, regtmp); 720f8bd822cSMark Brown if (ret != 0) 721f8bd822cSMark Brown return ret; 72275a5f89fSMark Brown continue; 723f8bd822cSMark Brown } 724f8bd822cSMark Brown 72575a5f89fSMark Brown if (!data) { 72675a5f89fSMark Brown data = regcache_get_val_addr(map, block, i); 72775a5f89fSMark Brown base = regtmp; 72875a5f89fSMark Brown } 72975a5f89fSMark Brown } 73075a5f89fSMark Brown 7312d49b598SLars-Peter Clausen return regcache_sync_block_raw_flush(map, &data, base, regtmp + 7322d49b598SLars-Peter Clausen map->reg_stride); 733f8bd822cSMark Brown } 734cfdeb8c3SMark Brown 735cfdeb8c3SMark Brown int regcache_sync_block(struct regmap *map, void *block, 7363f4ff561SLars-Peter Clausen unsigned long *cache_present, 737cfdeb8c3SMark Brown unsigned int block_base, unsigned int start, 738cfdeb8c3SMark Brown unsigned int end) 739cfdeb8c3SMark Brown { 74067921a1aSMarkus Pargmann if (regmap_can_raw_write(map) && !map->use_single_write) 7413f4ff561SLars-Peter Clausen return regcache_sync_block_raw(map, block, cache_present, 7423f4ff561SLars-Peter Clausen block_base, start, end); 743cfdeb8c3SMark Brown else 7443f4ff561SLars-Peter Clausen return regcache_sync_block_single(map, block, cache_present, 7453f4ff561SLars-Peter Clausen block_base, start, end); 746cfdeb8c3SMark Brown } 747