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) { 104*8cfe2fd3SXiubo Li if (config->reg_defaults || config->num_reg_defaults_raw) 105*8cfe2fd3SXiubo Li dev_warn(map->dev, 106*8cfe2fd3SXiubo Li "No cache used with register defaults set!\n"); 107*8cfe2fd3SXiubo Li 108e7a6db30SMark Brown map->cache_bypass = true; 1099fabe24eSDimitris Papastamos return 0; 110e7a6db30SMark Brown } 1119fabe24eSDimitris Papastamos 112*8cfe2fd3SXiubo Li for (i = 0; i < config->num_reg_defaults; i++) 113*8cfe2fd3SXiubo Li if (config->reg_defaults[i].reg % map->reg_stride) 114*8cfe2fd3SXiubo Li return -EINVAL; 115*8cfe2fd3SXiubo Li 1169fabe24eSDimitris Papastamos for (i = 0; i < ARRAY_SIZE(cache_types); i++) 1179fabe24eSDimitris Papastamos if (cache_types[i]->type == map->cache_type) 1189fabe24eSDimitris Papastamos break; 1199fabe24eSDimitris Papastamos 1209fabe24eSDimitris Papastamos if (i == ARRAY_SIZE(cache_types)) { 1219fabe24eSDimitris Papastamos dev_err(map->dev, "Could not match compress type: %d\n", 1229fabe24eSDimitris Papastamos map->cache_type); 1239fabe24eSDimitris Papastamos return -EINVAL; 1249fabe24eSDimitris Papastamos } 1259fabe24eSDimitris Papastamos 126e5e3b8abSLars-Peter Clausen map->num_reg_defaults = config->num_reg_defaults; 127e5e3b8abSLars-Peter Clausen map->num_reg_defaults_raw = config->num_reg_defaults_raw; 128e5e3b8abSLars-Peter Clausen map->reg_defaults_raw = config->reg_defaults_raw; 129064d4db1SLars-Peter Clausen map->cache_word_size = DIV_ROUND_UP(config->val_bits, 8); 130064d4db1SLars-Peter Clausen map->cache_size_raw = map->cache_word_size * config->num_reg_defaults_raw; 131e5e3b8abSLars-Peter Clausen 1329fabe24eSDimitris Papastamos map->cache = NULL; 1339fabe24eSDimitris Papastamos map->cache_ops = cache_types[i]; 1349fabe24eSDimitris Papastamos 1359fabe24eSDimitris Papastamos if (!map->cache_ops->read || 1369fabe24eSDimitris Papastamos !map->cache_ops->write || 1379fabe24eSDimitris Papastamos !map->cache_ops->name) 1389fabe24eSDimitris Papastamos return -EINVAL; 1399fabe24eSDimitris Papastamos 1409fabe24eSDimitris Papastamos /* We still need to ensure that the reg_defaults 1419fabe24eSDimitris Papastamos * won't vanish from under us. We'll need to make 1429fabe24eSDimitris Papastamos * a copy of it. 1439fabe24eSDimitris Papastamos */ 144720e4616SLars-Peter Clausen if (config->reg_defaults) { 1459fabe24eSDimitris Papastamos if (!map->num_reg_defaults) 1469fabe24eSDimitris Papastamos return -EINVAL; 147720e4616SLars-Peter Clausen tmp_buf = kmemdup(config->reg_defaults, map->num_reg_defaults * 1489fabe24eSDimitris Papastamos sizeof(struct reg_default), GFP_KERNEL); 1499fabe24eSDimitris Papastamos if (!tmp_buf) 1509fabe24eSDimitris Papastamos return -ENOMEM; 1519fabe24eSDimitris Papastamos map->reg_defaults = tmp_buf; 1528528bdd4SMark Brown } else if (map->num_reg_defaults_raw) { 1535fcd2560SMark Brown /* Some devices such as PMICs don't have cache defaults, 1549fabe24eSDimitris Papastamos * we cope with this by reading back the HW registers and 1559fabe24eSDimitris Papastamos * crafting the cache defaults by hand. 1569fabe24eSDimitris Papastamos */ 1579fabe24eSDimitris Papastamos ret = regcache_hw_init(map); 1589fabe24eSDimitris Papastamos if (ret < 0) 1599fabe24eSDimitris Papastamos return ret; 160fb70067eSXiubo Li if (map->cache_bypass) 161fb70067eSXiubo Li return 0; 1629fabe24eSDimitris Papastamos } 1639fabe24eSDimitris Papastamos 1649fabe24eSDimitris Papastamos if (!map->max_register) 1659fabe24eSDimitris Papastamos map->max_register = map->num_reg_defaults_raw; 1669fabe24eSDimitris Papastamos 1679fabe24eSDimitris Papastamos if (map->cache_ops->init) { 1689fabe24eSDimitris Papastamos dev_dbg(map->dev, "Initializing %s cache\n", 1699fabe24eSDimitris Papastamos map->cache_ops->name); 170bd061c78SLars-Peter Clausen ret = map->cache_ops->init(map); 171bd061c78SLars-Peter Clausen if (ret) 172bd061c78SLars-Peter Clausen goto err_free; 1739fabe24eSDimitris Papastamos } 1749fabe24eSDimitris Papastamos return 0; 175bd061c78SLars-Peter Clausen 176bd061c78SLars-Peter Clausen err_free: 177bd061c78SLars-Peter Clausen kfree(map->reg_defaults); 178bd061c78SLars-Peter Clausen if (map->cache_free) 179bd061c78SLars-Peter Clausen kfree(map->reg_defaults_raw); 180bd061c78SLars-Peter Clausen 181bd061c78SLars-Peter Clausen return ret; 1829fabe24eSDimitris Papastamos } 1839fabe24eSDimitris Papastamos 1849fabe24eSDimitris Papastamos void regcache_exit(struct regmap *map) 1859fabe24eSDimitris Papastamos { 1869fabe24eSDimitris Papastamos if (map->cache_type == REGCACHE_NONE) 1879fabe24eSDimitris Papastamos return; 1889fabe24eSDimitris Papastamos 1899fabe24eSDimitris Papastamos BUG_ON(!map->cache_ops); 1909fabe24eSDimitris Papastamos 1919fabe24eSDimitris Papastamos kfree(map->reg_defaults); 1929fabe24eSDimitris Papastamos if (map->cache_free) 1939fabe24eSDimitris Papastamos kfree(map->reg_defaults_raw); 1949fabe24eSDimitris Papastamos 1959fabe24eSDimitris Papastamos if (map->cache_ops->exit) { 1969fabe24eSDimitris Papastamos dev_dbg(map->dev, "Destroying %s cache\n", 1979fabe24eSDimitris Papastamos map->cache_ops->name); 1989fabe24eSDimitris Papastamos map->cache_ops->exit(map); 1999fabe24eSDimitris Papastamos } 2009fabe24eSDimitris Papastamos } 2019fabe24eSDimitris Papastamos 2029fabe24eSDimitris Papastamos /** 2039fabe24eSDimitris Papastamos * regcache_read: Fetch the value of a given register from the cache. 2049fabe24eSDimitris Papastamos * 2059fabe24eSDimitris Papastamos * @map: map to configure. 2069fabe24eSDimitris Papastamos * @reg: The register index. 2079fabe24eSDimitris Papastamos * @value: The value to be returned. 2089fabe24eSDimitris Papastamos * 2099fabe24eSDimitris Papastamos * Return a negative value on failure, 0 on success. 2109fabe24eSDimitris Papastamos */ 2119fabe24eSDimitris Papastamos int regcache_read(struct regmap *map, 2129fabe24eSDimitris Papastamos unsigned int reg, unsigned int *value) 2139fabe24eSDimitris Papastamos { 214bc7ee556SMark Brown int ret; 215bc7ee556SMark Brown 2169fabe24eSDimitris Papastamos if (map->cache_type == REGCACHE_NONE) 2179fabe24eSDimitris Papastamos return -ENOSYS; 2189fabe24eSDimitris Papastamos 2199fabe24eSDimitris Papastamos BUG_ON(!map->cache_ops); 2209fabe24eSDimitris Papastamos 221bc7ee556SMark Brown if (!regmap_volatile(map, reg)) { 222bc7ee556SMark Brown ret = map->cache_ops->read(map, reg, value); 223bc7ee556SMark Brown 224bc7ee556SMark Brown if (ret == 0) 225c6b570d9SPhilipp Zabel trace_regmap_reg_read_cache(map, reg, *value); 226bc7ee556SMark Brown 227bc7ee556SMark Brown return ret; 228bc7ee556SMark Brown } 2299fabe24eSDimitris Papastamos 2309fabe24eSDimitris Papastamos return -EINVAL; 2319fabe24eSDimitris Papastamos } 2329fabe24eSDimitris Papastamos 2339fabe24eSDimitris Papastamos /** 2349fabe24eSDimitris Papastamos * regcache_write: Set the value of a given register in the cache. 2359fabe24eSDimitris Papastamos * 2369fabe24eSDimitris Papastamos * @map: map to configure. 2379fabe24eSDimitris Papastamos * @reg: The register index. 2389fabe24eSDimitris Papastamos * @value: The new register value. 2399fabe24eSDimitris Papastamos * 2409fabe24eSDimitris Papastamos * Return a negative value on failure, 0 on success. 2419fabe24eSDimitris Papastamos */ 2429fabe24eSDimitris Papastamos int regcache_write(struct regmap *map, 2439fabe24eSDimitris Papastamos unsigned int reg, unsigned int value) 2449fabe24eSDimitris Papastamos { 2459fabe24eSDimitris Papastamos if (map->cache_type == REGCACHE_NONE) 2469fabe24eSDimitris Papastamos return 0; 2479fabe24eSDimitris Papastamos 2489fabe24eSDimitris Papastamos BUG_ON(!map->cache_ops); 2499fabe24eSDimitris Papastamos 2509fabe24eSDimitris Papastamos if (!regmap_volatile(map, reg)) 2519fabe24eSDimitris Papastamos return map->cache_ops->write(map, reg, value); 2529fabe24eSDimitris Papastamos 2539fabe24eSDimitris Papastamos return 0; 2549fabe24eSDimitris Papastamos } 2559fabe24eSDimitris Papastamos 2563969fa08SKevin Cernekee static bool regcache_reg_needs_sync(struct regmap *map, unsigned int reg, 2573969fa08SKevin Cernekee unsigned int val) 2583969fa08SKevin Cernekee { 2593969fa08SKevin Cernekee int ret; 2603969fa08SKevin Cernekee 2611c79771aSKevin Cernekee /* If we don't know the chip just got reset, then sync everything. */ 2621c79771aSKevin Cernekee if (!map->no_sync_defaults) 2631c79771aSKevin Cernekee return true; 2641c79771aSKevin Cernekee 2653969fa08SKevin Cernekee /* Is this the hardware default? If so skip. */ 2663969fa08SKevin Cernekee ret = regcache_lookup_reg(map, reg); 2673969fa08SKevin Cernekee if (ret >= 0 && val == map->reg_defaults[ret].def) 2683969fa08SKevin Cernekee return false; 2693969fa08SKevin Cernekee return true; 2703969fa08SKevin Cernekee } 2713969fa08SKevin Cernekee 272d856fce4SMaarten ter Huurne static int regcache_default_sync(struct regmap *map, unsigned int min, 273d856fce4SMaarten ter Huurne unsigned int max) 274d856fce4SMaarten ter Huurne { 275d856fce4SMaarten ter Huurne unsigned int reg; 276d856fce4SMaarten ter Huurne 27775617328SDylan Reid for (reg = min; reg <= max; reg += map->reg_stride) { 278d856fce4SMaarten ter Huurne unsigned int val; 279d856fce4SMaarten ter Huurne int ret; 280d856fce4SMaarten ter Huurne 28183f8475cSDylan Reid if (regmap_volatile(map, reg) || 28283f8475cSDylan Reid !regmap_writeable(map, reg)) 283d856fce4SMaarten ter Huurne continue; 284d856fce4SMaarten ter Huurne 285d856fce4SMaarten ter Huurne ret = regcache_read(map, reg, &val); 286d856fce4SMaarten ter Huurne if (ret) 287d856fce4SMaarten ter Huurne return ret; 288d856fce4SMaarten ter Huurne 2893969fa08SKevin Cernekee if (!regcache_reg_needs_sync(map, reg, val)) 290d856fce4SMaarten ter Huurne continue; 291d856fce4SMaarten ter Huurne 292621a5f7aSViresh Kumar map->cache_bypass = true; 293d856fce4SMaarten ter Huurne ret = _regmap_write(map, reg, val); 294621a5f7aSViresh Kumar map->cache_bypass = false; 295f29a4320SJarkko Nikula if (ret) { 296f29a4320SJarkko Nikula dev_err(map->dev, "Unable to sync register %#x. %d\n", 297f29a4320SJarkko Nikula reg, ret); 298d856fce4SMaarten ter Huurne return ret; 299f29a4320SJarkko Nikula } 300d856fce4SMaarten ter Huurne dev_dbg(map->dev, "Synced register %#x, value %#x\n", reg, val); 301d856fce4SMaarten ter Huurne } 302d856fce4SMaarten ter Huurne 303d856fce4SMaarten ter Huurne return 0; 304d856fce4SMaarten ter Huurne } 305d856fce4SMaarten ter Huurne 3069fabe24eSDimitris Papastamos /** 3079fabe24eSDimitris Papastamos * regcache_sync: Sync the register cache with the hardware. 3089fabe24eSDimitris Papastamos * 3099fabe24eSDimitris Papastamos * @map: map to configure. 3109fabe24eSDimitris Papastamos * 3119fabe24eSDimitris Papastamos * Any registers that should not be synced should be marked as 3129fabe24eSDimitris Papastamos * volatile. In general drivers can choose not to use the provided 3139fabe24eSDimitris Papastamos * syncing functionality if they so require. 3149fabe24eSDimitris Papastamos * 3159fabe24eSDimitris Papastamos * Return a negative value on failure, 0 on success. 3169fabe24eSDimitris Papastamos */ 3179fabe24eSDimitris Papastamos int regcache_sync(struct regmap *map) 3189fabe24eSDimitris Papastamos { 319954757d7SDimitris Papastamos int ret = 0; 320954757d7SDimitris Papastamos unsigned int i; 32159360089SDimitris Papastamos const char *name; 322621a5f7aSViresh Kumar bool bypass; 32359360089SDimitris Papastamos 324d856fce4SMaarten ter Huurne BUG_ON(!map->cache_ops); 3259fabe24eSDimitris Papastamos 32681485f52SLars-Peter Clausen map->lock(map->lock_arg); 327beb1a10fSDimitris Papastamos /* Remember the initial bypass state */ 328beb1a10fSDimitris Papastamos bypass = map->cache_bypass; 3299fabe24eSDimitris Papastamos dev_dbg(map->dev, "Syncing %s cache\n", 3309fabe24eSDimitris Papastamos map->cache_ops->name); 33159360089SDimitris Papastamos name = map->cache_ops->name; 332c6b570d9SPhilipp Zabel trace_regcache_sync(map, name, "start"); 33322f0d90aSMark Brown 3348ae0d7e8SMark Brown if (!map->cache_dirty) 3358ae0d7e8SMark Brown goto out; 336d9db7627SMark Brown 337affbe886SMark Brown map->async = true; 338affbe886SMark Brown 33922f0d90aSMark Brown /* Apply any patch first */ 340621a5f7aSViresh Kumar map->cache_bypass = true; 34122f0d90aSMark Brown for (i = 0; i < map->patch_regs; i++) { 34222f0d90aSMark Brown ret = _regmap_write(map, map->patch[i].reg, map->patch[i].def); 34322f0d90aSMark Brown if (ret != 0) { 34422f0d90aSMark Brown dev_err(map->dev, "Failed to write %x = %x: %d\n", 34522f0d90aSMark Brown map->patch[i].reg, map->patch[i].def, ret); 34622f0d90aSMark Brown goto out; 34722f0d90aSMark Brown } 34822f0d90aSMark Brown } 349621a5f7aSViresh Kumar map->cache_bypass = false; 35022f0d90aSMark Brown 351d856fce4SMaarten ter Huurne if (map->cache_ops->sync) 352ac8d91c8SMark Brown ret = map->cache_ops->sync(map, 0, map->max_register); 353d856fce4SMaarten ter Huurne else 354d856fce4SMaarten ter Huurne ret = regcache_default_sync(map, 0, map->max_register); 355954757d7SDimitris Papastamos 3566ff73738SMark Brown if (ret == 0) 3576ff73738SMark Brown map->cache_dirty = false; 3586ff73738SMark Brown 359954757d7SDimitris Papastamos out: 360beb1a10fSDimitris Papastamos /* Restore the bypass state */ 361affbe886SMark Brown map->async = false; 362beb1a10fSDimitris Papastamos map->cache_bypass = bypass; 3631c79771aSKevin Cernekee map->no_sync_defaults = false; 36481485f52SLars-Peter Clausen map->unlock(map->lock_arg); 365954757d7SDimitris Papastamos 366affbe886SMark Brown regmap_async_complete(map); 367affbe886SMark Brown 368c6b570d9SPhilipp Zabel trace_regcache_sync(map, name, "stop"); 369affbe886SMark Brown 370954757d7SDimitris Papastamos return ret; 3719fabe24eSDimitris Papastamos } 3729fabe24eSDimitris Papastamos EXPORT_SYMBOL_GPL(regcache_sync); 3739fabe24eSDimitris Papastamos 37492afb286SMark Brown /** 3754d4cfd16SMark Brown * regcache_sync_region: Sync part of the register cache with the hardware. 3764d4cfd16SMark Brown * 3774d4cfd16SMark Brown * @map: map to sync. 3784d4cfd16SMark Brown * @min: first register to sync 3794d4cfd16SMark Brown * @max: last register to sync 3804d4cfd16SMark Brown * 3814d4cfd16SMark Brown * Write all non-default register values in the specified region to 3824d4cfd16SMark Brown * the hardware. 3834d4cfd16SMark Brown * 3844d4cfd16SMark Brown * Return a negative value on failure, 0 on success. 3854d4cfd16SMark Brown */ 3864d4cfd16SMark Brown int regcache_sync_region(struct regmap *map, unsigned int min, 3874d4cfd16SMark Brown unsigned int max) 3884d4cfd16SMark Brown { 3894d4cfd16SMark Brown int ret = 0; 3904d4cfd16SMark Brown const char *name; 391621a5f7aSViresh Kumar bool bypass; 3924d4cfd16SMark Brown 393d856fce4SMaarten ter Huurne BUG_ON(!map->cache_ops); 3944d4cfd16SMark Brown 39581485f52SLars-Peter Clausen map->lock(map->lock_arg); 3964d4cfd16SMark Brown 3974d4cfd16SMark Brown /* Remember the initial bypass state */ 3984d4cfd16SMark Brown bypass = map->cache_bypass; 3994d4cfd16SMark Brown 4004d4cfd16SMark Brown name = map->cache_ops->name; 4014d4cfd16SMark Brown dev_dbg(map->dev, "Syncing %s cache from %d-%d\n", name, min, max); 4024d4cfd16SMark Brown 403c6b570d9SPhilipp Zabel trace_regcache_sync(map, name, "start region"); 4044d4cfd16SMark Brown 4054d4cfd16SMark Brown if (!map->cache_dirty) 4064d4cfd16SMark Brown goto out; 4074d4cfd16SMark Brown 408affbe886SMark Brown map->async = true; 409affbe886SMark Brown 410d856fce4SMaarten ter Huurne if (map->cache_ops->sync) 4114d4cfd16SMark Brown ret = map->cache_ops->sync(map, min, max); 412d856fce4SMaarten ter Huurne else 413d856fce4SMaarten ter Huurne ret = regcache_default_sync(map, min, max); 4144d4cfd16SMark Brown 4154d4cfd16SMark Brown out: 4164d4cfd16SMark Brown /* Restore the bypass state */ 4174d4cfd16SMark Brown map->cache_bypass = bypass; 418affbe886SMark Brown map->async = false; 4191c79771aSKevin Cernekee map->no_sync_defaults = false; 42081485f52SLars-Peter Clausen map->unlock(map->lock_arg); 4214d4cfd16SMark Brown 422affbe886SMark Brown regmap_async_complete(map); 423affbe886SMark Brown 424c6b570d9SPhilipp Zabel trace_regcache_sync(map, name, "stop region"); 425affbe886SMark Brown 4264d4cfd16SMark Brown return ret; 4274d4cfd16SMark Brown } 428e466de05SMark Brown EXPORT_SYMBOL_GPL(regcache_sync_region); 4294d4cfd16SMark Brown 4304d4cfd16SMark Brown /** 431697e85bcSMark Brown * regcache_drop_region: Discard part of the register cache 432697e85bcSMark Brown * 433697e85bcSMark Brown * @map: map to operate on 434697e85bcSMark Brown * @min: first register to discard 435697e85bcSMark Brown * @max: last register to discard 436697e85bcSMark Brown * 437697e85bcSMark Brown * Discard part of the register cache. 438697e85bcSMark Brown * 439697e85bcSMark Brown * Return a negative value on failure, 0 on success. 440697e85bcSMark Brown */ 441697e85bcSMark Brown int regcache_drop_region(struct regmap *map, unsigned int min, 442697e85bcSMark Brown unsigned int max) 443697e85bcSMark Brown { 444697e85bcSMark Brown int ret = 0; 445697e85bcSMark Brown 4463f4ff561SLars-Peter Clausen if (!map->cache_ops || !map->cache_ops->drop) 447697e85bcSMark Brown return -EINVAL; 448697e85bcSMark Brown 44981485f52SLars-Peter Clausen map->lock(map->lock_arg); 450697e85bcSMark Brown 451c6b570d9SPhilipp Zabel trace_regcache_drop_region(map, min, max); 452697e85bcSMark Brown 453697e85bcSMark Brown ret = map->cache_ops->drop(map, min, max); 454697e85bcSMark Brown 45581485f52SLars-Peter Clausen map->unlock(map->lock_arg); 456697e85bcSMark Brown 457697e85bcSMark Brown return ret; 458697e85bcSMark Brown } 459697e85bcSMark Brown EXPORT_SYMBOL_GPL(regcache_drop_region); 460697e85bcSMark Brown 461697e85bcSMark Brown /** 46292afb286SMark Brown * regcache_cache_only: Put a register map into cache only mode 46392afb286SMark Brown * 46492afb286SMark Brown * @map: map to configure 46592afb286SMark Brown * @cache_only: flag if changes should be written to the hardware 46692afb286SMark Brown * 46792afb286SMark Brown * When a register map is marked as cache only writes to the register 46892afb286SMark Brown * map API will only update the register cache, they will not cause 46992afb286SMark Brown * any hardware changes. This is useful for allowing portions of 47092afb286SMark Brown * drivers to act as though the device were functioning as normal when 47192afb286SMark Brown * it is disabled for power saving reasons. 47292afb286SMark Brown */ 47392afb286SMark Brown void regcache_cache_only(struct regmap *map, bool enable) 47492afb286SMark Brown { 47581485f52SLars-Peter Clausen map->lock(map->lock_arg); 476ac77a765SDimitris Papastamos WARN_ON(map->cache_bypass && enable); 47792afb286SMark Brown map->cache_only = enable; 478c6b570d9SPhilipp Zabel trace_regmap_cache_only(map, enable); 47981485f52SLars-Peter Clausen map->unlock(map->lock_arg); 48092afb286SMark Brown } 48192afb286SMark Brown EXPORT_SYMBOL_GPL(regcache_cache_only); 48292afb286SMark Brown 4836eb0f5e0SDimitris Papastamos /** 4841c79771aSKevin Cernekee * regcache_mark_dirty: Indicate that HW registers were reset to default values 4858ae0d7e8SMark Brown * 4868ae0d7e8SMark Brown * @map: map to mark 4878ae0d7e8SMark Brown * 4881c79771aSKevin Cernekee * Inform regcache that the device has been powered down or reset, so that 4891c79771aSKevin Cernekee * on resume, regcache_sync() knows to write out all non-default values 4901c79771aSKevin Cernekee * stored in the cache. 4911c79771aSKevin Cernekee * 4921c79771aSKevin Cernekee * If this function is not called, regcache_sync() will assume that 4931c79771aSKevin Cernekee * the hardware state still matches the cache state, modulo any writes that 4941c79771aSKevin Cernekee * happened when cache_only was true. 4958ae0d7e8SMark Brown */ 4968ae0d7e8SMark Brown void regcache_mark_dirty(struct regmap *map) 4978ae0d7e8SMark Brown { 49881485f52SLars-Peter Clausen map->lock(map->lock_arg); 4998ae0d7e8SMark Brown map->cache_dirty = true; 5001c79771aSKevin Cernekee map->no_sync_defaults = true; 50181485f52SLars-Peter Clausen map->unlock(map->lock_arg); 5028ae0d7e8SMark Brown } 5038ae0d7e8SMark Brown EXPORT_SYMBOL_GPL(regcache_mark_dirty); 5048ae0d7e8SMark Brown 5058ae0d7e8SMark Brown /** 5066eb0f5e0SDimitris Papastamos * regcache_cache_bypass: Put a register map into cache bypass mode 5076eb0f5e0SDimitris Papastamos * 5086eb0f5e0SDimitris Papastamos * @map: map to configure 5090eef6b04SDimitris Papastamos * @cache_bypass: flag if changes should not be written to the hardware 5106eb0f5e0SDimitris Papastamos * 5116eb0f5e0SDimitris Papastamos * When a register map is marked with the cache bypass option, writes 5126eb0f5e0SDimitris Papastamos * to the register map API will only update the hardware and not the 5136eb0f5e0SDimitris Papastamos * the cache directly. This is useful when syncing the cache back to 5146eb0f5e0SDimitris Papastamos * the hardware. 5156eb0f5e0SDimitris Papastamos */ 5166eb0f5e0SDimitris Papastamos void regcache_cache_bypass(struct regmap *map, bool enable) 5176eb0f5e0SDimitris Papastamos { 51881485f52SLars-Peter Clausen map->lock(map->lock_arg); 519ac77a765SDimitris Papastamos WARN_ON(map->cache_only && enable); 5206eb0f5e0SDimitris Papastamos map->cache_bypass = enable; 521c6b570d9SPhilipp Zabel trace_regmap_cache_bypass(map, enable); 52281485f52SLars-Peter Clausen map->unlock(map->lock_arg); 5236eb0f5e0SDimitris Papastamos } 5246eb0f5e0SDimitris Papastamos EXPORT_SYMBOL_GPL(regcache_cache_bypass); 5256eb0f5e0SDimitris Papastamos 526879082c9SMark Brown bool regcache_set_val(struct regmap *map, void *base, unsigned int idx, 527879082c9SMark Brown unsigned int val) 5289fabe24eSDimitris Papastamos { 529325acab4SMark Brown if (regcache_get_val(map, base, idx) == val) 530325acab4SMark Brown return true; 531325acab4SMark Brown 532eb4cb76fSMark Brown /* Use device native format if possible */ 533eb4cb76fSMark Brown if (map->format.format_val) { 534eb4cb76fSMark Brown map->format.format_val(base + (map->cache_word_size * idx), 535eb4cb76fSMark Brown val, 0); 536eb4cb76fSMark Brown return false; 537eb4cb76fSMark Brown } 538eb4cb76fSMark Brown 539879082c9SMark Brown switch (map->cache_word_size) { 5409fabe24eSDimitris Papastamos case 1: { 5419fabe24eSDimitris Papastamos u8 *cache = base; 5429fabe24eSDimitris Papastamos cache[idx] = val; 5439fabe24eSDimitris Papastamos break; 5449fabe24eSDimitris Papastamos } 5459fabe24eSDimitris Papastamos case 2: { 5469fabe24eSDimitris Papastamos u16 *cache = base; 5479fabe24eSDimitris Papastamos cache[idx] = val; 5489fabe24eSDimitris Papastamos break; 5499fabe24eSDimitris Papastamos } 5507d5e525bSMark Brown case 4: { 5517d5e525bSMark Brown u32 *cache = base; 5527d5e525bSMark Brown cache[idx] = val; 5537d5e525bSMark Brown break; 5547d5e525bSMark Brown } 5559fabe24eSDimitris Papastamos default: 5569fabe24eSDimitris Papastamos BUG(); 5579fabe24eSDimitris Papastamos } 5589fabe24eSDimitris Papastamos return false; 5599fabe24eSDimitris Papastamos } 5609fabe24eSDimitris Papastamos 561879082c9SMark Brown unsigned int regcache_get_val(struct regmap *map, const void *base, 562879082c9SMark Brown unsigned int idx) 5639fabe24eSDimitris Papastamos { 5649fabe24eSDimitris Papastamos if (!base) 5659fabe24eSDimitris Papastamos return -EINVAL; 5669fabe24eSDimitris Papastamos 567eb4cb76fSMark Brown /* Use device native format if possible */ 568eb4cb76fSMark Brown if (map->format.parse_val) 5698817796bSMark Brown return map->format.parse_val(regcache_get_val_addr(map, base, 5708817796bSMark Brown idx)); 571eb4cb76fSMark Brown 572879082c9SMark Brown switch (map->cache_word_size) { 5739fabe24eSDimitris Papastamos case 1: { 5749fabe24eSDimitris Papastamos const u8 *cache = base; 5759fabe24eSDimitris Papastamos return cache[idx]; 5769fabe24eSDimitris Papastamos } 5779fabe24eSDimitris Papastamos case 2: { 5789fabe24eSDimitris Papastamos const u16 *cache = base; 5799fabe24eSDimitris Papastamos return cache[idx]; 5809fabe24eSDimitris Papastamos } 5817d5e525bSMark Brown case 4: { 5827d5e525bSMark Brown const u32 *cache = base; 5837d5e525bSMark Brown return cache[idx]; 5847d5e525bSMark Brown } 5859fabe24eSDimitris Papastamos default: 5869fabe24eSDimitris Papastamos BUG(); 5879fabe24eSDimitris Papastamos } 5889fabe24eSDimitris Papastamos /* unreachable */ 5899fabe24eSDimitris Papastamos return -1; 5909fabe24eSDimitris Papastamos } 5919fabe24eSDimitris Papastamos 592f094fea6SMark Brown static int regcache_default_cmp(const void *a, const void *b) 593c08604b8SDimitris Papastamos { 594c08604b8SDimitris Papastamos const struct reg_default *_a = a; 595c08604b8SDimitris Papastamos const struct reg_default *_b = b; 596c08604b8SDimitris Papastamos 597c08604b8SDimitris Papastamos return _a->reg - _b->reg; 598c08604b8SDimitris Papastamos } 599c08604b8SDimitris Papastamos 600f094fea6SMark Brown int regcache_lookup_reg(struct regmap *map, unsigned int reg) 601f094fea6SMark Brown { 602f094fea6SMark Brown struct reg_default key; 603f094fea6SMark Brown struct reg_default *r; 604f094fea6SMark Brown 605f094fea6SMark Brown key.reg = reg; 606f094fea6SMark Brown key.def = 0; 607f094fea6SMark Brown 608f094fea6SMark Brown r = bsearch(&key, map->reg_defaults, map->num_reg_defaults, 609f094fea6SMark Brown sizeof(struct reg_default), regcache_default_cmp); 610f094fea6SMark Brown 611f094fea6SMark Brown if (r) 612f094fea6SMark Brown return r - map->reg_defaults; 613f094fea6SMark Brown else 6146e6ace00SMark Brown return -ENOENT; 615f094fea6SMark Brown } 616f8bd822cSMark Brown 6173f4ff561SLars-Peter Clausen static bool regcache_reg_present(unsigned long *cache_present, unsigned int idx) 6183f4ff561SLars-Peter Clausen { 6193f4ff561SLars-Peter Clausen if (!cache_present) 6203f4ff561SLars-Peter Clausen return true; 6213f4ff561SLars-Peter Clausen 6223f4ff561SLars-Peter Clausen return test_bit(idx, cache_present); 6233f4ff561SLars-Peter Clausen } 6243f4ff561SLars-Peter Clausen 625cfdeb8c3SMark Brown static int regcache_sync_block_single(struct regmap *map, void *block, 6263f4ff561SLars-Peter Clausen unsigned long *cache_present, 627cfdeb8c3SMark Brown unsigned int block_base, 628cfdeb8c3SMark Brown unsigned int start, unsigned int end) 629cfdeb8c3SMark Brown { 630cfdeb8c3SMark Brown unsigned int i, regtmp, val; 631cfdeb8c3SMark Brown int ret; 632cfdeb8c3SMark Brown 633cfdeb8c3SMark Brown for (i = start; i < end; i++) { 634cfdeb8c3SMark Brown regtmp = block_base + (i * map->reg_stride); 635cfdeb8c3SMark Brown 6364ceba98dSTakashi Iwai if (!regcache_reg_present(cache_present, i) || 6374ceba98dSTakashi Iwai !regmap_writeable(map, regtmp)) 638cfdeb8c3SMark Brown continue; 639cfdeb8c3SMark Brown 640cfdeb8c3SMark Brown val = regcache_get_val(map, block, i); 6413969fa08SKevin Cernekee if (!regcache_reg_needs_sync(map, regtmp, val)) 642cfdeb8c3SMark Brown continue; 643cfdeb8c3SMark Brown 644621a5f7aSViresh Kumar map->cache_bypass = true; 645cfdeb8c3SMark Brown 646cfdeb8c3SMark Brown ret = _regmap_write(map, regtmp, val); 647cfdeb8c3SMark Brown 648621a5f7aSViresh Kumar map->cache_bypass = false; 649f29a4320SJarkko Nikula if (ret != 0) { 650f29a4320SJarkko Nikula dev_err(map->dev, "Unable to sync register %#x. %d\n", 651f29a4320SJarkko Nikula regtmp, ret); 652cfdeb8c3SMark Brown return ret; 653f29a4320SJarkko Nikula } 654cfdeb8c3SMark Brown dev_dbg(map->dev, "Synced register %#x, value %#x\n", 655cfdeb8c3SMark Brown regtmp, val); 656cfdeb8c3SMark Brown } 657cfdeb8c3SMark Brown 658cfdeb8c3SMark Brown return 0; 659cfdeb8c3SMark Brown } 660cfdeb8c3SMark Brown 66175a5f89fSMark Brown static int regcache_sync_block_raw_flush(struct regmap *map, const void **data, 66275a5f89fSMark Brown unsigned int base, unsigned int cur) 66375a5f89fSMark Brown { 66475a5f89fSMark Brown size_t val_bytes = map->format.val_bytes; 66575a5f89fSMark Brown int ret, count; 66675a5f89fSMark Brown 66775a5f89fSMark Brown if (*data == NULL) 66875a5f89fSMark Brown return 0; 66975a5f89fSMark Brown 67078ba73eeSDylan Reid count = (cur - base) / map->reg_stride; 67175a5f89fSMark Brown 6729659293cSStratos Karafotis dev_dbg(map->dev, "Writing %zu bytes for %d registers from 0x%x-0x%x\n", 67378ba73eeSDylan Reid count * val_bytes, count, base, cur - map->reg_stride); 67475a5f89fSMark Brown 675621a5f7aSViresh Kumar map->cache_bypass = true; 67675a5f89fSMark Brown 6770a819809SMark Brown ret = _regmap_raw_write(map, base, *data, count * val_bytes); 678f29a4320SJarkko Nikula if (ret) 679f29a4320SJarkko Nikula dev_err(map->dev, "Unable to sync registers %#x-%#x. %d\n", 680f29a4320SJarkko Nikula base, cur - map->reg_stride, ret); 68175a5f89fSMark Brown 682621a5f7aSViresh Kumar map->cache_bypass = false; 68375a5f89fSMark Brown 68475a5f89fSMark Brown *data = NULL; 68575a5f89fSMark Brown 68675a5f89fSMark Brown return ret; 68775a5f89fSMark Brown } 68875a5f89fSMark Brown 689f52687afSSachin Kamat static int regcache_sync_block_raw(struct regmap *map, void *block, 6903f4ff561SLars-Peter Clausen unsigned long *cache_present, 691f8bd822cSMark Brown unsigned int block_base, unsigned int start, 692f8bd822cSMark Brown unsigned int end) 693f8bd822cSMark Brown { 69475a5f89fSMark Brown unsigned int i, val; 69575a5f89fSMark Brown unsigned int regtmp = 0; 69675a5f89fSMark Brown unsigned int base = 0; 69775a5f89fSMark Brown const void *data = NULL; 698f8bd822cSMark Brown int ret; 699f8bd822cSMark Brown 700f8bd822cSMark Brown for (i = start; i < end; i++) { 701f8bd822cSMark Brown regtmp = block_base + (i * map->reg_stride); 702f8bd822cSMark Brown 7034ceba98dSTakashi Iwai if (!regcache_reg_present(cache_present, i) || 7044ceba98dSTakashi Iwai !regmap_writeable(map, regtmp)) { 70575a5f89fSMark Brown ret = regcache_sync_block_raw_flush(map, &data, 70675a5f89fSMark Brown base, regtmp); 70775a5f89fSMark Brown if (ret != 0) 70875a5f89fSMark Brown return ret; 709f8bd822cSMark Brown continue; 71075a5f89fSMark Brown } 711f8bd822cSMark Brown 712f8bd822cSMark Brown val = regcache_get_val(map, block, i); 7133969fa08SKevin Cernekee if (!regcache_reg_needs_sync(map, regtmp, val)) { 71475a5f89fSMark Brown ret = regcache_sync_block_raw_flush(map, &data, 71575a5f89fSMark Brown base, regtmp); 716f8bd822cSMark Brown if (ret != 0) 717f8bd822cSMark Brown return ret; 71875a5f89fSMark Brown continue; 719f8bd822cSMark Brown } 720f8bd822cSMark Brown 72175a5f89fSMark Brown if (!data) { 72275a5f89fSMark Brown data = regcache_get_val_addr(map, block, i); 72375a5f89fSMark Brown base = regtmp; 72475a5f89fSMark Brown } 72575a5f89fSMark Brown } 72675a5f89fSMark Brown 7272d49b598SLars-Peter Clausen return regcache_sync_block_raw_flush(map, &data, base, regtmp + 7282d49b598SLars-Peter Clausen map->reg_stride); 729f8bd822cSMark Brown } 730cfdeb8c3SMark Brown 731cfdeb8c3SMark Brown int regcache_sync_block(struct regmap *map, void *block, 7323f4ff561SLars-Peter Clausen unsigned long *cache_present, 733cfdeb8c3SMark Brown unsigned int block_base, unsigned int start, 734cfdeb8c3SMark Brown unsigned int end) 735cfdeb8c3SMark Brown { 73667921a1aSMarkus Pargmann if (regmap_can_raw_write(map) && !map->use_single_write) 7373f4ff561SLars-Peter Clausen return regcache_sync_block_raw(map, block, cache_present, 7383f4ff561SLars-Peter Clausen block_base, start, end); 739cfdeb8c3SMark Brown else 7403f4ff561SLars-Peter Clausen return regcache_sync_block_single(map, block, cache_present, 7413f4ff561SLars-Peter Clausen block_base, start, end); 742cfdeb8c3SMark Brown } 743