137613fa5SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 237613fa5SGreg Kroah-Hartman // 337613fa5SGreg Kroah-Hartman // Register cache access API 437613fa5SGreg Kroah-Hartman // 537613fa5SGreg Kroah-Hartman // Copyright 2011 Wolfson Microelectronics plc 637613fa5SGreg Kroah-Hartman // 737613fa5SGreg Kroah-Hartman // Author: Dimitris Papastamos <dp@opensource.wolfsonmicro.com> 89fabe24eSDimitris Papastamos 9f094fea6SMark Brown #include <linux/bsearch.h> 10e39be3a3SXiubo Li #include <linux/device.h> 11e39be3a3SXiubo Li #include <linux/export.h> 12e39be3a3SXiubo Li #include <linux/slab.h> 13c08604b8SDimitris Papastamos #include <linux/sort.h> 149fabe24eSDimitris Papastamos 15f58078daSSteven Rostedt #include "trace.h" 169fabe24eSDimitris Papastamos #include "internal.h" 179fabe24eSDimitris Papastamos 189fabe24eSDimitris Papastamos static const struct regcache_ops *cache_types[] = { 1928644c80SDimitris Papastamos ®cache_rbtree_ops, 20f458e610SMark Brown #if IS_ENABLED(CONFIG_REGCACHE_COMPRESSED) 212cbbb579SDimitris Papastamos ®cache_lzo_ops, 2234a730aaSJonas Gorski #endif 232ac902ceSMark Brown ®cache_flat_ops, 249fabe24eSDimitris Papastamos }; 259fabe24eSDimitris Papastamos 269fabe24eSDimitris Papastamos static int regcache_hw_init(struct regmap *map) 279fabe24eSDimitris Papastamos { 289fabe24eSDimitris Papastamos int i, j; 299fabe24eSDimitris Papastamos int ret; 309fabe24eSDimitris Papastamos int count; 313245d460SMark Brown unsigned int reg, val; 329fabe24eSDimitris Papastamos void *tmp_buf; 339fabe24eSDimitris Papastamos 349fabe24eSDimitris Papastamos if (!map->num_reg_defaults_raw) 359fabe24eSDimitris Papastamos return -EINVAL; 369fabe24eSDimitris Papastamos 37fb70067eSXiubo Li /* calculate the size of reg_defaults */ 38fb70067eSXiubo Li for (count = 0, i = 0; i < map->num_reg_defaults_raw; i++) 39b2c7f5d9SMaarten ter Huurne if (regmap_readable(map, i * map->reg_stride) && 40b2c7f5d9SMaarten ter Huurne !regmap_volatile(map, i * map->reg_stride)) 41fb70067eSXiubo Li count++; 42fb70067eSXiubo Li 43b2c7f5d9SMaarten ter Huurne /* all registers are unreadable or volatile, so just bypass */ 44fb70067eSXiubo Li if (!count) { 45fb70067eSXiubo Li map->cache_bypass = true; 46fb70067eSXiubo Li return 0; 47fb70067eSXiubo Li } 48fb70067eSXiubo Li 49fb70067eSXiubo Li map->num_reg_defaults = count; 50fb70067eSXiubo Li map->reg_defaults = kmalloc_array(count, sizeof(struct reg_default), 51fb70067eSXiubo Li GFP_KERNEL); 52fb70067eSXiubo Li if (!map->reg_defaults) 53fb70067eSXiubo Li return -ENOMEM; 54fb70067eSXiubo Li 559fabe24eSDimitris Papastamos if (!map->reg_defaults_raw) { 56621a5f7aSViresh Kumar bool cache_bypass = map->cache_bypass; 579fabe24eSDimitris Papastamos dev_warn(map->dev, "No cache defaults, reading back from HW\n"); 58df00c79fSLaxman Dewangan 59df00c79fSLaxman Dewangan /* Bypass the cache access till data read from HW */ 60621a5f7aSViresh Kumar map->cache_bypass = true; 619fabe24eSDimitris Papastamos tmp_buf = kmalloc(map->cache_size_raw, GFP_KERNEL); 62fb70067eSXiubo Li if (!tmp_buf) { 63fb70067eSXiubo Li ret = -ENOMEM; 64fb70067eSXiubo Li goto err_free; 65fb70067eSXiubo Li } 66eb4cb76fSMark Brown ret = regmap_raw_read(map, 0, tmp_buf, 67d51fe1f3SMaciej S. Szmigiero map->cache_size_raw); 68df00c79fSLaxman Dewangan map->cache_bypass = cache_bypass; 693245d460SMark Brown if (ret == 0) { 709fabe24eSDimitris Papastamos map->reg_defaults_raw = tmp_buf; 71b67498d6SJiapeng Zhong map->cache_free = true; 723245d460SMark Brown } else { 733245d460SMark Brown kfree(tmp_buf); 743245d460SMark Brown } 759fabe24eSDimitris Papastamos } 769fabe24eSDimitris Papastamos 779fabe24eSDimitris Papastamos /* fill the reg_defaults */ 789fabe24eSDimitris Papastamos for (i = 0, j = 0; i < map->num_reg_defaults_raw; i++) { 793245d460SMark Brown reg = i * map->reg_stride; 803245d460SMark Brown 813245d460SMark Brown if (!regmap_readable(map, reg)) 829fabe24eSDimitris Papastamos continue; 833245d460SMark Brown 843245d460SMark Brown if (regmap_volatile(map, reg)) 853245d460SMark Brown continue; 863245d460SMark Brown 873245d460SMark Brown if (map->reg_defaults_raw) { 88fbba43c5SXiubo Li val = regcache_get_val(map, map->reg_defaults_raw, i); 893245d460SMark Brown } else { 903245d460SMark Brown bool cache_bypass = map->cache_bypass; 913245d460SMark Brown 923245d460SMark Brown map->cache_bypass = true; 933245d460SMark Brown ret = regmap_read(map, reg, &val); 943245d460SMark Brown map->cache_bypass = cache_bypass; 953245d460SMark Brown if (ret != 0) { 963245d460SMark Brown dev_err(map->dev, "Failed to read %d: %d\n", 973245d460SMark Brown reg, ret); 983245d460SMark Brown goto err_free; 993245d460SMark Brown } 1003245d460SMark Brown } 1013245d460SMark Brown 1023245d460SMark Brown map->reg_defaults[j].reg = reg; 1039fabe24eSDimitris Papastamos map->reg_defaults[j].def = val; 1049fabe24eSDimitris Papastamos j++; 1059fabe24eSDimitris Papastamos } 1069fabe24eSDimitris Papastamos 1079fabe24eSDimitris Papastamos return 0; 108021cd616SLars-Peter Clausen 109021cd616SLars-Peter Clausen err_free: 110fb70067eSXiubo Li kfree(map->reg_defaults); 111021cd616SLars-Peter Clausen 112021cd616SLars-Peter Clausen return ret; 1139fabe24eSDimitris Papastamos } 1149fabe24eSDimitris Papastamos 115e5e3b8abSLars-Peter Clausen int regcache_init(struct regmap *map, const struct regmap_config *config) 1169fabe24eSDimitris Papastamos { 1179fabe24eSDimitris Papastamos int ret; 1189fabe24eSDimitris Papastamos int i; 1199fabe24eSDimitris Papastamos void *tmp_buf; 1209fabe24eSDimitris Papastamos 121e7a6db30SMark Brown if (map->cache_type == REGCACHE_NONE) { 1228cfe2fd3SXiubo Li if (config->reg_defaults || config->num_reg_defaults_raw) 1238cfe2fd3SXiubo Li dev_warn(map->dev, 1248cfe2fd3SXiubo Li "No cache used with register defaults set!\n"); 1258cfe2fd3SXiubo Li 126e7a6db30SMark Brown map->cache_bypass = true; 1279fabe24eSDimitris Papastamos return 0; 128e7a6db30SMark Brown } 1299fabe24eSDimitris Papastamos 130167f7066SXiubo Li if (config->reg_defaults && !config->num_reg_defaults) { 131167f7066SXiubo Li dev_err(map->dev, 132167f7066SXiubo Li "Register defaults are set without the number!\n"); 133167f7066SXiubo Li return -EINVAL; 134167f7066SXiubo Li } 135167f7066SXiubo Li 1368cfe2fd3SXiubo Li for (i = 0; i < config->num_reg_defaults; i++) 1378cfe2fd3SXiubo Li if (config->reg_defaults[i].reg % map->reg_stride) 1388cfe2fd3SXiubo Li return -EINVAL; 1398cfe2fd3SXiubo Li 1409fabe24eSDimitris Papastamos for (i = 0; i < ARRAY_SIZE(cache_types); i++) 1419fabe24eSDimitris Papastamos if (cache_types[i]->type == map->cache_type) 1429fabe24eSDimitris Papastamos break; 1439fabe24eSDimitris Papastamos 1449fabe24eSDimitris Papastamos if (i == ARRAY_SIZE(cache_types)) { 1459fabe24eSDimitris Papastamos dev_err(map->dev, "Could not match compress type: %d\n", 1469fabe24eSDimitris Papastamos map->cache_type); 1479fabe24eSDimitris Papastamos return -EINVAL; 1489fabe24eSDimitris Papastamos } 1499fabe24eSDimitris Papastamos 150e5e3b8abSLars-Peter Clausen map->num_reg_defaults = config->num_reg_defaults; 151e5e3b8abSLars-Peter Clausen map->num_reg_defaults_raw = config->num_reg_defaults_raw; 152e5e3b8abSLars-Peter Clausen map->reg_defaults_raw = config->reg_defaults_raw; 153064d4db1SLars-Peter Clausen map->cache_word_size = DIV_ROUND_UP(config->val_bits, 8); 154064d4db1SLars-Peter Clausen map->cache_size_raw = map->cache_word_size * config->num_reg_defaults_raw; 155e5e3b8abSLars-Peter Clausen 1569fabe24eSDimitris Papastamos map->cache = NULL; 1579fabe24eSDimitris Papastamos map->cache_ops = cache_types[i]; 1589fabe24eSDimitris Papastamos 1599fabe24eSDimitris Papastamos if (!map->cache_ops->read || 1609fabe24eSDimitris Papastamos !map->cache_ops->write || 1619fabe24eSDimitris Papastamos !map->cache_ops->name) 1629fabe24eSDimitris Papastamos return -EINVAL; 1639fabe24eSDimitris Papastamos 1649fabe24eSDimitris Papastamos /* We still need to ensure that the reg_defaults 1659fabe24eSDimitris Papastamos * won't vanish from under us. We'll need to make 1669fabe24eSDimitris Papastamos * a copy of it. 1679fabe24eSDimitris Papastamos */ 168720e4616SLars-Peter Clausen if (config->reg_defaults) { 169720e4616SLars-Peter Clausen tmp_buf = kmemdup(config->reg_defaults, map->num_reg_defaults * 1709fabe24eSDimitris Papastamos sizeof(struct reg_default), GFP_KERNEL); 1719fabe24eSDimitris Papastamos if (!tmp_buf) 1729fabe24eSDimitris Papastamos return -ENOMEM; 1739fabe24eSDimitris Papastamos map->reg_defaults = tmp_buf; 1748528bdd4SMark Brown } else if (map->num_reg_defaults_raw) { 1755fcd2560SMark Brown /* Some devices such as PMICs don't have cache defaults, 1769fabe24eSDimitris Papastamos * we cope with this by reading back the HW registers and 1779fabe24eSDimitris Papastamos * crafting the cache defaults by hand. 1789fabe24eSDimitris Papastamos */ 1799fabe24eSDimitris Papastamos ret = regcache_hw_init(map); 1809fabe24eSDimitris Papastamos if (ret < 0) 1819fabe24eSDimitris Papastamos return ret; 182fb70067eSXiubo Li if (map->cache_bypass) 183fb70067eSXiubo Li return 0; 1849fabe24eSDimitris Papastamos } 1859fabe24eSDimitris Papastamos 186*d6409475SJeongtae Park if (!map->max_register && map->num_reg_defaults_raw) 187*d6409475SJeongtae Park map->max_register = (map->num_reg_defaults_raw - 1) * map->reg_stride; 1889fabe24eSDimitris Papastamos 1899fabe24eSDimitris Papastamos if (map->cache_ops->init) { 1909fabe24eSDimitris Papastamos dev_dbg(map->dev, "Initializing %s cache\n", 1919fabe24eSDimitris Papastamos map->cache_ops->name); 192bd061c78SLars-Peter Clausen ret = map->cache_ops->init(map); 193bd061c78SLars-Peter Clausen if (ret) 194bd061c78SLars-Peter Clausen goto err_free; 1959fabe24eSDimitris Papastamos } 1969fabe24eSDimitris Papastamos return 0; 197bd061c78SLars-Peter Clausen 198bd061c78SLars-Peter Clausen err_free: 199bd061c78SLars-Peter Clausen kfree(map->reg_defaults); 200bd061c78SLars-Peter Clausen if (map->cache_free) 201bd061c78SLars-Peter Clausen kfree(map->reg_defaults_raw); 202bd061c78SLars-Peter Clausen 203bd061c78SLars-Peter Clausen return ret; 2049fabe24eSDimitris Papastamos } 2059fabe24eSDimitris Papastamos 2069fabe24eSDimitris Papastamos void regcache_exit(struct regmap *map) 2079fabe24eSDimitris Papastamos { 2089fabe24eSDimitris Papastamos if (map->cache_type == REGCACHE_NONE) 2099fabe24eSDimitris Papastamos return; 2109fabe24eSDimitris Papastamos 2119fabe24eSDimitris Papastamos BUG_ON(!map->cache_ops); 2129fabe24eSDimitris Papastamos 2139fabe24eSDimitris Papastamos kfree(map->reg_defaults); 2149fabe24eSDimitris Papastamos if (map->cache_free) 2159fabe24eSDimitris Papastamos kfree(map->reg_defaults_raw); 2169fabe24eSDimitris Papastamos 2179fabe24eSDimitris Papastamos if (map->cache_ops->exit) { 2189fabe24eSDimitris Papastamos dev_dbg(map->dev, "Destroying %s cache\n", 2199fabe24eSDimitris Papastamos map->cache_ops->name); 2209fabe24eSDimitris Papastamos map->cache_ops->exit(map); 2219fabe24eSDimitris Papastamos } 2229fabe24eSDimitris Papastamos } 2239fabe24eSDimitris Papastamos 2249fabe24eSDimitris Papastamos /** 2252cf8e2dfSCharles Keepax * regcache_read - Fetch the value of a given register from the cache. 2269fabe24eSDimitris Papastamos * 2279fabe24eSDimitris Papastamos * @map: map to configure. 2289fabe24eSDimitris Papastamos * @reg: The register index. 2299fabe24eSDimitris Papastamos * @value: The value to be returned. 2309fabe24eSDimitris Papastamos * 2319fabe24eSDimitris Papastamos * Return a negative value on failure, 0 on success. 2329fabe24eSDimitris Papastamos */ 2339fabe24eSDimitris Papastamos int regcache_read(struct regmap *map, 2349fabe24eSDimitris Papastamos unsigned int reg, unsigned int *value) 2359fabe24eSDimitris Papastamos { 236bc7ee556SMark Brown int ret; 237bc7ee556SMark Brown 2389fabe24eSDimitris Papastamos if (map->cache_type == REGCACHE_NONE) 2399fabe24eSDimitris Papastamos return -ENOSYS; 2409fabe24eSDimitris Papastamos 2419fabe24eSDimitris Papastamos BUG_ON(!map->cache_ops); 2429fabe24eSDimitris Papastamos 243bc7ee556SMark Brown if (!regmap_volatile(map, reg)) { 244bc7ee556SMark Brown ret = map->cache_ops->read(map, reg, value); 245bc7ee556SMark Brown 246bc7ee556SMark Brown if (ret == 0) 247c6b570d9SPhilipp Zabel trace_regmap_reg_read_cache(map, reg, *value); 248bc7ee556SMark Brown 249bc7ee556SMark Brown return ret; 250bc7ee556SMark Brown } 2519fabe24eSDimitris Papastamos 2529fabe24eSDimitris Papastamos return -EINVAL; 2539fabe24eSDimitris Papastamos } 2549fabe24eSDimitris Papastamos 2559fabe24eSDimitris Papastamos /** 2562cf8e2dfSCharles Keepax * regcache_write - Set the value of a given register in the cache. 2579fabe24eSDimitris Papastamos * 2589fabe24eSDimitris Papastamos * @map: map to configure. 2599fabe24eSDimitris Papastamos * @reg: The register index. 2609fabe24eSDimitris Papastamos * @value: The new register value. 2619fabe24eSDimitris Papastamos * 2629fabe24eSDimitris Papastamos * Return a negative value on failure, 0 on success. 2639fabe24eSDimitris Papastamos */ 2649fabe24eSDimitris Papastamos int regcache_write(struct regmap *map, 2659fabe24eSDimitris Papastamos unsigned int reg, unsigned int value) 2669fabe24eSDimitris Papastamos { 2679fabe24eSDimitris Papastamos if (map->cache_type == REGCACHE_NONE) 2689fabe24eSDimitris Papastamos return 0; 2699fabe24eSDimitris Papastamos 2709fabe24eSDimitris Papastamos BUG_ON(!map->cache_ops); 2719fabe24eSDimitris Papastamos 2729fabe24eSDimitris Papastamos if (!regmap_volatile(map, reg)) 2739fabe24eSDimitris Papastamos return map->cache_ops->write(map, reg, value); 2749fabe24eSDimitris Papastamos 2759fabe24eSDimitris Papastamos return 0; 2769fabe24eSDimitris Papastamos } 2779fabe24eSDimitris Papastamos 2783969fa08SKevin Cernekee static bool regcache_reg_needs_sync(struct regmap *map, unsigned int reg, 2793969fa08SKevin Cernekee unsigned int val) 2803969fa08SKevin Cernekee { 2813969fa08SKevin Cernekee int ret; 2823969fa08SKevin Cernekee 2831c79771aSKevin Cernekee /* If we don't know the chip just got reset, then sync everything. */ 2841c79771aSKevin Cernekee if (!map->no_sync_defaults) 2851c79771aSKevin Cernekee return true; 2861c79771aSKevin Cernekee 2873969fa08SKevin Cernekee /* Is this the hardware default? If so skip. */ 2883969fa08SKevin Cernekee ret = regcache_lookup_reg(map, reg); 2893969fa08SKevin Cernekee if (ret >= 0 && val == map->reg_defaults[ret].def) 2903969fa08SKevin Cernekee return false; 2913969fa08SKevin Cernekee return true; 2923969fa08SKevin Cernekee } 2933969fa08SKevin Cernekee 294d856fce4SMaarten ter Huurne static int regcache_default_sync(struct regmap *map, unsigned int min, 295d856fce4SMaarten ter Huurne unsigned int max) 296d856fce4SMaarten ter Huurne { 297d856fce4SMaarten ter Huurne unsigned int reg; 298d856fce4SMaarten ter Huurne 29975617328SDylan Reid for (reg = min; reg <= max; reg += map->reg_stride) { 300d856fce4SMaarten ter Huurne unsigned int val; 301d856fce4SMaarten ter Huurne int ret; 302d856fce4SMaarten ter Huurne 30383f8475cSDylan Reid if (regmap_volatile(map, reg) || 30483f8475cSDylan Reid !regmap_writeable(map, reg)) 305d856fce4SMaarten ter Huurne continue; 306d856fce4SMaarten ter Huurne 307d856fce4SMaarten ter Huurne ret = regcache_read(map, reg, &val); 308d856fce4SMaarten ter Huurne if (ret) 309d856fce4SMaarten ter Huurne return ret; 310d856fce4SMaarten ter Huurne 3113969fa08SKevin Cernekee if (!regcache_reg_needs_sync(map, reg, val)) 312d856fce4SMaarten ter Huurne continue; 313d856fce4SMaarten ter Huurne 314621a5f7aSViresh Kumar map->cache_bypass = true; 315d856fce4SMaarten ter Huurne ret = _regmap_write(map, reg, val); 316621a5f7aSViresh Kumar map->cache_bypass = false; 317f29a4320SJarkko Nikula if (ret) { 318f29a4320SJarkko Nikula dev_err(map->dev, "Unable to sync register %#x. %d\n", 319f29a4320SJarkko Nikula reg, ret); 320d856fce4SMaarten ter Huurne return ret; 321f29a4320SJarkko Nikula } 322d856fce4SMaarten ter Huurne dev_dbg(map->dev, "Synced register %#x, value %#x\n", reg, val); 323d856fce4SMaarten ter Huurne } 324d856fce4SMaarten ter Huurne 325d856fce4SMaarten ter Huurne return 0; 326d856fce4SMaarten ter Huurne } 327d856fce4SMaarten ter Huurne 3289fabe24eSDimitris Papastamos /** 3292cf8e2dfSCharles Keepax * regcache_sync - Sync the register cache with the hardware. 3309fabe24eSDimitris Papastamos * 3319fabe24eSDimitris Papastamos * @map: map to configure. 3329fabe24eSDimitris Papastamos * 3339fabe24eSDimitris Papastamos * Any registers that should not be synced should be marked as 3349fabe24eSDimitris Papastamos * volatile. In general drivers can choose not to use the provided 3359fabe24eSDimitris Papastamos * syncing functionality if they so require. 3369fabe24eSDimitris Papastamos * 3379fabe24eSDimitris Papastamos * Return a negative value on failure, 0 on success. 3389fabe24eSDimitris Papastamos */ 3399fabe24eSDimitris Papastamos int regcache_sync(struct regmap *map) 3409fabe24eSDimitris Papastamos { 341954757d7SDimitris Papastamos int ret = 0; 342954757d7SDimitris Papastamos unsigned int i; 34359360089SDimitris Papastamos const char *name; 344621a5f7aSViresh Kumar bool bypass; 34559360089SDimitris Papastamos 346d856fce4SMaarten ter Huurne BUG_ON(!map->cache_ops); 3479fabe24eSDimitris Papastamos 34881485f52SLars-Peter Clausen map->lock(map->lock_arg); 349beb1a10fSDimitris Papastamos /* Remember the initial bypass state */ 350beb1a10fSDimitris Papastamos bypass = map->cache_bypass; 3519fabe24eSDimitris Papastamos dev_dbg(map->dev, "Syncing %s cache\n", 3529fabe24eSDimitris Papastamos map->cache_ops->name); 35359360089SDimitris Papastamos name = map->cache_ops->name; 354c6b570d9SPhilipp Zabel trace_regcache_sync(map, name, "start"); 35522f0d90aSMark Brown 3568ae0d7e8SMark Brown if (!map->cache_dirty) 3578ae0d7e8SMark Brown goto out; 358d9db7627SMark Brown 359affbe886SMark Brown map->async = true; 360affbe886SMark Brown 36122f0d90aSMark Brown /* Apply any patch first */ 362621a5f7aSViresh Kumar map->cache_bypass = true; 36322f0d90aSMark Brown for (i = 0; i < map->patch_regs; i++) { 36422f0d90aSMark Brown ret = _regmap_write(map, map->patch[i].reg, map->patch[i].def); 36522f0d90aSMark Brown if (ret != 0) { 36622f0d90aSMark Brown dev_err(map->dev, "Failed to write %x = %x: %d\n", 36722f0d90aSMark Brown map->patch[i].reg, map->patch[i].def, ret); 36822f0d90aSMark Brown goto out; 36922f0d90aSMark Brown } 37022f0d90aSMark Brown } 371621a5f7aSViresh Kumar map->cache_bypass = false; 37222f0d90aSMark Brown 373d856fce4SMaarten ter Huurne if (map->cache_ops->sync) 374ac8d91c8SMark Brown ret = map->cache_ops->sync(map, 0, map->max_register); 375d856fce4SMaarten ter Huurne else 376d856fce4SMaarten ter Huurne ret = regcache_default_sync(map, 0, map->max_register); 377954757d7SDimitris Papastamos 3786ff73738SMark Brown if (ret == 0) 3796ff73738SMark Brown map->cache_dirty = false; 3806ff73738SMark Brown 381954757d7SDimitris Papastamos out: 382beb1a10fSDimitris Papastamos /* Restore the bypass state */ 383affbe886SMark Brown map->async = false; 384beb1a10fSDimitris Papastamos map->cache_bypass = bypass; 3851c79771aSKevin Cernekee map->no_sync_defaults = false; 38681485f52SLars-Peter Clausen map->unlock(map->lock_arg); 387954757d7SDimitris Papastamos 388affbe886SMark Brown regmap_async_complete(map); 389affbe886SMark Brown 390c6b570d9SPhilipp Zabel trace_regcache_sync(map, name, "stop"); 391affbe886SMark Brown 392954757d7SDimitris Papastamos return ret; 3939fabe24eSDimitris Papastamos } 3949fabe24eSDimitris Papastamos EXPORT_SYMBOL_GPL(regcache_sync); 3959fabe24eSDimitris Papastamos 39692afb286SMark Brown /** 3972cf8e2dfSCharles Keepax * regcache_sync_region - Sync part of the register cache with the hardware. 3984d4cfd16SMark Brown * 3994d4cfd16SMark Brown * @map: map to sync. 4004d4cfd16SMark Brown * @min: first register to sync 4014d4cfd16SMark Brown * @max: last register to sync 4024d4cfd16SMark Brown * 4034d4cfd16SMark Brown * Write all non-default register values in the specified region to 4044d4cfd16SMark Brown * the hardware. 4054d4cfd16SMark Brown * 4064d4cfd16SMark Brown * Return a negative value on failure, 0 on success. 4074d4cfd16SMark Brown */ 4084d4cfd16SMark Brown int regcache_sync_region(struct regmap *map, unsigned int min, 4094d4cfd16SMark Brown unsigned int max) 4104d4cfd16SMark Brown { 4114d4cfd16SMark Brown int ret = 0; 4124d4cfd16SMark Brown const char *name; 413621a5f7aSViresh Kumar bool bypass; 4144d4cfd16SMark Brown 415d856fce4SMaarten ter Huurne BUG_ON(!map->cache_ops); 4164d4cfd16SMark Brown 41781485f52SLars-Peter Clausen map->lock(map->lock_arg); 4184d4cfd16SMark Brown 4194d4cfd16SMark Brown /* Remember the initial bypass state */ 4204d4cfd16SMark Brown bypass = map->cache_bypass; 4214d4cfd16SMark Brown 4224d4cfd16SMark Brown name = map->cache_ops->name; 4234d4cfd16SMark Brown dev_dbg(map->dev, "Syncing %s cache from %d-%d\n", name, min, max); 4244d4cfd16SMark Brown 425c6b570d9SPhilipp Zabel trace_regcache_sync(map, name, "start region"); 4264d4cfd16SMark Brown 4274d4cfd16SMark Brown if (!map->cache_dirty) 4284d4cfd16SMark Brown goto out; 4294d4cfd16SMark Brown 430affbe886SMark Brown map->async = true; 431affbe886SMark Brown 432d856fce4SMaarten ter Huurne if (map->cache_ops->sync) 4334d4cfd16SMark Brown ret = map->cache_ops->sync(map, min, max); 434d856fce4SMaarten ter Huurne else 435d856fce4SMaarten ter Huurne ret = regcache_default_sync(map, min, max); 4364d4cfd16SMark Brown 4374d4cfd16SMark Brown out: 4384d4cfd16SMark Brown /* Restore the bypass state */ 4394d4cfd16SMark Brown map->cache_bypass = bypass; 440affbe886SMark Brown map->async = false; 4411c79771aSKevin Cernekee map->no_sync_defaults = false; 44281485f52SLars-Peter Clausen map->unlock(map->lock_arg); 4434d4cfd16SMark Brown 444affbe886SMark Brown regmap_async_complete(map); 445affbe886SMark Brown 446c6b570d9SPhilipp Zabel trace_regcache_sync(map, name, "stop region"); 447affbe886SMark Brown 4484d4cfd16SMark Brown return ret; 4494d4cfd16SMark Brown } 450e466de05SMark Brown EXPORT_SYMBOL_GPL(regcache_sync_region); 4514d4cfd16SMark Brown 4524d4cfd16SMark Brown /** 4532cf8e2dfSCharles Keepax * regcache_drop_region - Discard part of the register cache 454697e85bcSMark Brown * 455697e85bcSMark Brown * @map: map to operate on 456697e85bcSMark Brown * @min: first register to discard 457697e85bcSMark Brown * @max: last register to discard 458697e85bcSMark Brown * 459697e85bcSMark Brown * Discard part of the register cache. 460697e85bcSMark Brown * 461697e85bcSMark Brown * Return a negative value on failure, 0 on success. 462697e85bcSMark Brown */ 463697e85bcSMark Brown int regcache_drop_region(struct regmap *map, unsigned int min, 464697e85bcSMark Brown unsigned int max) 465697e85bcSMark Brown { 466697e85bcSMark Brown int ret = 0; 467697e85bcSMark Brown 4683f4ff561SLars-Peter Clausen if (!map->cache_ops || !map->cache_ops->drop) 469697e85bcSMark Brown return -EINVAL; 470697e85bcSMark Brown 47181485f52SLars-Peter Clausen map->lock(map->lock_arg); 472697e85bcSMark Brown 473c6b570d9SPhilipp Zabel trace_regcache_drop_region(map, min, max); 474697e85bcSMark Brown 475697e85bcSMark Brown ret = map->cache_ops->drop(map, min, max); 476697e85bcSMark Brown 47781485f52SLars-Peter Clausen map->unlock(map->lock_arg); 478697e85bcSMark Brown 479697e85bcSMark Brown return ret; 480697e85bcSMark Brown } 481697e85bcSMark Brown EXPORT_SYMBOL_GPL(regcache_drop_region); 482697e85bcSMark Brown 483697e85bcSMark Brown /** 4842cf8e2dfSCharles Keepax * regcache_cache_only - Put a register map into cache only mode 48592afb286SMark Brown * 48692afb286SMark Brown * @map: map to configure 4872cf8e2dfSCharles Keepax * @enable: flag if changes should be written to the hardware 48892afb286SMark Brown * 48992afb286SMark Brown * When a register map is marked as cache only writes to the register 49092afb286SMark Brown * map API will only update the register cache, they will not cause 49192afb286SMark Brown * any hardware changes. This is useful for allowing portions of 49292afb286SMark Brown * drivers to act as though the device were functioning as normal when 49392afb286SMark Brown * it is disabled for power saving reasons. 49492afb286SMark Brown */ 49592afb286SMark Brown void regcache_cache_only(struct regmap *map, bool enable) 49692afb286SMark Brown { 49781485f52SLars-Peter Clausen map->lock(map->lock_arg); 498ac77a765SDimitris Papastamos WARN_ON(map->cache_bypass && enable); 49992afb286SMark Brown map->cache_only = enable; 500c6b570d9SPhilipp Zabel trace_regmap_cache_only(map, enable); 50181485f52SLars-Peter Clausen map->unlock(map->lock_arg); 50292afb286SMark Brown } 50392afb286SMark Brown EXPORT_SYMBOL_GPL(regcache_cache_only); 50492afb286SMark Brown 5056eb0f5e0SDimitris Papastamos /** 5062cf8e2dfSCharles Keepax * regcache_mark_dirty - Indicate that HW registers were reset to default values 5078ae0d7e8SMark Brown * 5088ae0d7e8SMark Brown * @map: map to mark 5098ae0d7e8SMark Brown * 5101c79771aSKevin Cernekee * Inform regcache that the device has been powered down or reset, so that 5111c79771aSKevin Cernekee * on resume, regcache_sync() knows to write out all non-default values 5121c79771aSKevin Cernekee * stored in the cache. 5131c79771aSKevin Cernekee * 5141c79771aSKevin Cernekee * If this function is not called, regcache_sync() will assume that 5151c79771aSKevin Cernekee * the hardware state still matches the cache state, modulo any writes that 5161c79771aSKevin Cernekee * happened when cache_only was true. 5178ae0d7e8SMark Brown */ 5188ae0d7e8SMark Brown void regcache_mark_dirty(struct regmap *map) 5198ae0d7e8SMark Brown { 52081485f52SLars-Peter Clausen map->lock(map->lock_arg); 5218ae0d7e8SMark Brown map->cache_dirty = true; 5221c79771aSKevin Cernekee map->no_sync_defaults = true; 52381485f52SLars-Peter Clausen map->unlock(map->lock_arg); 5248ae0d7e8SMark Brown } 5258ae0d7e8SMark Brown EXPORT_SYMBOL_GPL(regcache_mark_dirty); 5268ae0d7e8SMark Brown 5278ae0d7e8SMark Brown /** 5282cf8e2dfSCharles Keepax * regcache_cache_bypass - Put a register map into cache bypass mode 5296eb0f5e0SDimitris Papastamos * 5306eb0f5e0SDimitris Papastamos * @map: map to configure 5312cf8e2dfSCharles Keepax * @enable: flag if changes should not be written to the cache 5326eb0f5e0SDimitris Papastamos * 5336eb0f5e0SDimitris Papastamos * When a register map is marked with the cache bypass option, writes 5346eb0f5e0SDimitris Papastamos * to the register map API will only update the hardware and not the 5356eb0f5e0SDimitris Papastamos * the cache directly. This is useful when syncing the cache back to 5366eb0f5e0SDimitris Papastamos * the hardware. 5376eb0f5e0SDimitris Papastamos */ 5386eb0f5e0SDimitris Papastamos void regcache_cache_bypass(struct regmap *map, bool enable) 5396eb0f5e0SDimitris Papastamos { 54081485f52SLars-Peter Clausen map->lock(map->lock_arg); 541ac77a765SDimitris Papastamos WARN_ON(map->cache_only && enable); 5426eb0f5e0SDimitris Papastamos map->cache_bypass = enable; 543c6b570d9SPhilipp Zabel trace_regmap_cache_bypass(map, enable); 54481485f52SLars-Peter Clausen map->unlock(map->lock_arg); 5456eb0f5e0SDimitris Papastamos } 5466eb0f5e0SDimitris Papastamos EXPORT_SYMBOL_GPL(regcache_cache_bypass); 5476eb0f5e0SDimitris Papastamos 548879082c9SMark Brown bool regcache_set_val(struct regmap *map, void *base, unsigned int idx, 549879082c9SMark Brown unsigned int val) 5509fabe24eSDimitris Papastamos { 551325acab4SMark Brown if (regcache_get_val(map, base, idx) == val) 552325acab4SMark Brown return true; 553325acab4SMark Brown 554eb4cb76fSMark Brown /* Use device native format if possible */ 555eb4cb76fSMark Brown if (map->format.format_val) { 556eb4cb76fSMark Brown map->format.format_val(base + (map->cache_word_size * idx), 557eb4cb76fSMark Brown val, 0); 558eb4cb76fSMark Brown return false; 559eb4cb76fSMark Brown } 560eb4cb76fSMark Brown 561879082c9SMark Brown switch (map->cache_word_size) { 5629fabe24eSDimitris Papastamos case 1: { 5639fabe24eSDimitris Papastamos u8 *cache = base; 5642fd6902eSXiubo Li 5659fabe24eSDimitris Papastamos cache[idx] = val; 5669fabe24eSDimitris Papastamos break; 5679fabe24eSDimitris Papastamos } 5689fabe24eSDimitris Papastamos case 2: { 5699fabe24eSDimitris Papastamos u16 *cache = base; 5702fd6902eSXiubo Li 5719fabe24eSDimitris Papastamos cache[idx] = val; 5729fabe24eSDimitris Papastamos break; 5739fabe24eSDimitris Papastamos } 5747d5e525bSMark Brown case 4: { 5757d5e525bSMark Brown u32 *cache = base; 5762fd6902eSXiubo Li 5777d5e525bSMark Brown cache[idx] = val; 5787d5e525bSMark Brown break; 5797d5e525bSMark Brown } 5808b7663deSXiubo Li #ifdef CONFIG_64BIT 5818b7663deSXiubo Li case 8: { 5828b7663deSXiubo Li u64 *cache = base; 5838b7663deSXiubo Li 5848b7663deSXiubo Li cache[idx] = val; 5858b7663deSXiubo Li break; 5868b7663deSXiubo Li } 5878b7663deSXiubo Li #endif 5889fabe24eSDimitris Papastamos default: 5899fabe24eSDimitris Papastamos BUG(); 5909fabe24eSDimitris Papastamos } 5919fabe24eSDimitris Papastamos return false; 5929fabe24eSDimitris Papastamos } 5939fabe24eSDimitris Papastamos 594879082c9SMark Brown unsigned int regcache_get_val(struct regmap *map, const void *base, 595879082c9SMark Brown unsigned int idx) 5969fabe24eSDimitris Papastamos { 5979fabe24eSDimitris Papastamos if (!base) 5989fabe24eSDimitris Papastamos return -EINVAL; 5999fabe24eSDimitris Papastamos 600eb4cb76fSMark Brown /* Use device native format if possible */ 601eb4cb76fSMark Brown if (map->format.parse_val) 6028817796bSMark Brown return map->format.parse_val(regcache_get_val_addr(map, base, 6038817796bSMark Brown idx)); 604eb4cb76fSMark Brown 605879082c9SMark Brown switch (map->cache_word_size) { 6069fabe24eSDimitris Papastamos case 1: { 6079fabe24eSDimitris Papastamos const u8 *cache = base; 6082fd6902eSXiubo Li 6099fabe24eSDimitris Papastamos return cache[idx]; 6109fabe24eSDimitris Papastamos } 6119fabe24eSDimitris Papastamos case 2: { 6129fabe24eSDimitris Papastamos const u16 *cache = base; 6132fd6902eSXiubo Li 6149fabe24eSDimitris Papastamos return cache[idx]; 6159fabe24eSDimitris Papastamos } 6167d5e525bSMark Brown case 4: { 6177d5e525bSMark Brown const u32 *cache = base; 6182fd6902eSXiubo Li 6197d5e525bSMark Brown return cache[idx]; 6207d5e525bSMark Brown } 6218b7663deSXiubo Li #ifdef CONFIG_64BIT 6228b7663deSXiubo Li case 8: { 6238b7663deSXiubo Li const u64 *cache = base; 6248b7663deSXiubo Li 6258b7663deSXiubo Li return cache[idx]; 6268b7663deSXiubo Li } 6278b7663deSXiubo Li #endif 6289fabe24eSDimitris Papastamos default: 6299fabe24eSDimitris Papastamos BUG(); 6309fabe24eSDimitris Papastamos } 6319fabe24eSDimitris Papastamos /* unreachable */ 6329fabe24eSDimitris Papastamos return -1; 6339fabe24eSDimitris Papastamos } 6349fabe24eSDimitris Papastamos 635f094fea6SMark Brown static int regcache_default_cmp(const void *a, const void *b) 636c08604b8SDimitris Papastamos { 637c08604b8SDimitris Papastamos const struct reg_default *_a = a; 638c08604b8SDimitris Papastamos const struct reg_default *_b = b; 639c08604b8SDimitris Papastamos 640c08604b8SDimitris Papastamos return _a->reg - _b->reg; 641c08604b8SDimitris Papastamos } 642c08604b8SDimitris Papastamos 643f094fea6SMark Brown int regcache_lookup_reg(struct regmap *map, unsigned int reg) 644f094fea6SMark Brown { 645f094fea6SMark Brown struct reg_default key; 646f094fea6SMark Brown struct reg_default *r; 647f094fea6SMark Brown 648f094fea6SMark Brown key.reg = reg; 649f094fea6SMark Brown key.def = 0; 650f094fea6SMark Brown 651f094fea6SMark Brown r = bsearch(&key, map->reg_defaults, map->num_reg_defaults, 652f094fea6SMark Brown sizeof(struct reg_default), regcache_default_cmp); 653f094fea6SMark Brown 654f094fea6SMark Brown if (r) 655f094fea6SMark Brown return r - map->reg_defaults; 656f094fea6SMark Brown else 6576e6ace00SMark Brown return -ENOENT; 658f094fea6SMark Brown } 659f8bd822cSMark Brown 6603f4ff561SLars-Peter Clausen static bool regcache_reg_present(unsigned long *cache_present, unsigned int idx) 6613f4ff561SLars-Peter Clausen { 6623f4ff561SLars-Peter Clausen if (!cache_present) 6633f4ff561SLars-Peter Clausen return true; 6643f4ff561SLars-Peter Clausen 6653f4ff561SLars-Peter Clausen return test_bit(idx, cache_present); 6663f4ff561SLars-Peter Clausen } 6673f4ff561SLars-Peter Clausen 668cfdeb8c3SMark Brown static int regcache_sync_block_single(struct regmap *map, void *block, 6693f4ff561SLars-Peter Clausen unsigned long *cache_present, 670cfdeb8c3SMark Brown unsigned int block_base, 671cfdeb8c3SMark Brown unsigned int start, unsigned int end) 672cfdeb8c3SMark Brown { 673cfdeb8c3SMark Brown unsigned int i, regtmp, val; 674cfdeb8c3SMark Brown int ret; 675cfdeb8c3SMark Brown 676cfdeb8c3SMark Brown for (i = start; i < end; i++) { 677cfdeb8c3SMark Brown regtmp = block_base + (i * map->reg_stride); 678cfdeb8c3SMark Brown 6794ceba98dSTakashi Iwai if (!regcache_reg_present(cache_present, i) || 6804ceba98dSTakashi Iwai !regmap_writeable(map, regtmp)) 681cfdeb8c3SMark Brown continue; 682cfdeb8c3SMark Brown 683cfdeb8c3SMark Brown val = regcache_get_val(map, block, i); 6843969fa08SKevin Cernekee if (!regcache_reg_needs_sync(map, regtmp, val)) 685cfdeb8c3SMark Brown continue; 686cfdeb8c3SMark Brown 687621a5f7aSViresh Kumar map->cache_bypass = true; 688cfdeb8c3SMark Brown 689cfdeb8c3SMark Brown ret = _regmap_write(map, regtmp, val); 690cfdeb8c3SMark Brown 691621a5f7aSViresh Kumar map->cache_bypass = false; 692f29a4320SJarkko Nikula if (ret != 0) { 693f29a4320SJarkko Nikula dev_err(map->dev, "Unable to sync register %#x. %d\n", 694f29a4320SJarkko Nikula regtmp, ret); 695cfdeb8c3SMark Brown return ret; 696f29a4320SJarkko Nikula } 697cfdeb8c3SMark Brown dev_dbg(map->dev, "Synced register %#x, value %#x\n", 698cfdeb8c3SMark Brown regtmp, val); 699cfdeb8c3SMark Brown } 700cfdeb8c3SMark Brown 701cfdeb8c3SMark Brown return 0; 702cfdeb8c3SMark Brown } 703cfdeb8c3SMark Brown 70475a5f89fSMark Brown static int regcache_sync_block_raw_flush(struct regmap *map, const void **data, 70575a5f89fSMark Brown unsigned int base, unsigned int cur) 70675a5f89fSMark Brown { 70775a5f89fSMark Brown size_t val_bytes = map->format.val_bytes; 70875a5f89fSMark Brown int ret, count; 70975a5f89fSMark Brown 71075a5f89fSMark Brown if (*data == NULL) 71175a5f89fSMark Brown return 0; 71275a5f89fSMark Brown 71378ba73eeSDylan Reid count = (cur - base) / map->reg_stride; 71475a5f89fSMark Brown 7159659293cSStratos Karafotis dev_dbg(map->dev, "Writing %zu bytes for %d registers from 0x%x-0x%x\n", 71678ba73eeSDylan Reid count * val_bytes, count, base, cur - map->reg_stride); 71775a5f89fSMark Brown 718621a5f7aSViresh Kumar map->cache_bypass = true; 71975a5f89fSMark Brown 72005669b63SDmitry Baryshkov ret = _regmap_raw_write(map, base, *data, count * val_bytes, false); 721f29a4320SJarkko Nikula if (ret) 722f29a4320SJarkko Nikula dev_err(map->dev, "Unable to sync registers %#x-%#x. %d\n", 723f29a4320SJarkko Nikula base, cur - map->reg_stride, ret); 72475a5f89fSMark Brown 725621a5f7aSViresh Kumar map->cache_bypass = false; 72675a5f89fSMark Brown 72775a5f89fSMark Brown *data = NULL; 72875a5f89fSMark Brown 72975a5f89fSMark Brown return ret; 73075a5f89fSMark Brown } 73175a5f89fSMark Brown 732f52687afSSachin Kamat static int regcache_sync_block_raw(struct regmap *map, void *block, 7333f4ff561SLars-Peter Clausen unsigned long *cache_present, 734f8bd822cSMark Brown unsigned int block_base, unsigned int start, 735f8bd822cSMark Brown unsigned int end) 736f8bd822cSMark Brown { 73775a5f89fSMark Brown unsigned int i, val; 73875a5f89fSMark Brown unsigned int regtmp = 0; 73975a5f89fSMark Brown unsigned int base = 0; 74075a5f89fSMark Brown const void *data = NULL; 741f8bd822cSMark Brown int ret; 742f8bd822cSMark Brown 743f8bd822cSMark Brown for (i = start; i < end; i++) { 744f8bd822cSMark Brown regtmp = block_base + (i * map->reg_stride); 745f8bd822cSMark Brown 7464ceba98dSTakashi Iwai if (!regcache_reg_present(cache_present, i) || 7474ceba98dSTakashi Iwai !regmap_writeable(map, regtmp)) { 74875a5f89fSMark Brown ret = regcache_sync_block_raw_flush(map, &data, 74975a5f89fSMark Brown base, regtmp); 75075a5f89fSMark Brown if (ret != 0) 75175a5f89fSMark Brown return ret; 752f8bd822cSMark Brown continue; 75375a5f89fSMark Brown } 754f8bd822cSMark Brown 755f8bd822cSMark Brown val = regcache_get_val(map, block, i); 7563969fa08SKevin Cernekee if (!regcache_reg_needs_sync(map, regtmp, val)) { 75775a5f89fSMark Brown ret = regcache_sync_block_raw_flush(map, &data, 75875a5f89fSMark Brown base, regtmp); 759f8bd822cSMark Brown if (ret != 0) 760f8bd822cSMark Brown return ret; 76175a5f89fSMark Brown continue; 762f8bd822cSMark Brown } 763f8bd822cSMark Brown 76475a5f89fSMark Brown if (!data) { 76575a5f89fSMark Brown data = regcache_get_val_addr(map, block, i); 76675a5f89fSMark Brown base = regtmp; 76775a5f89fSMark Brown } 76875a5f89fSMark Brown } 76975a5f89fSMark Brown 7702d49b598SLars-Peter Clausen return regcache_sync_block_raw_flush(map, &data, base, regtmp + 7712d49b598SLars-Peter Clausen map->reg_stride); 772f8bd822cSMark Brown } 773cfdeb8c3SMark Brown 774cfdeb8c3SMark Brown int regcache_sync_block(struct regmap *map, void *block, 7753f4ff561SLars-Peter Clausen unsigned long *cache_present, 776cfdeb8c3SMark Brown unsigned int block_base, unsigned int start, 777cfdeb8c3SMark Brown unsigned int end) 778cfdeb8c3SMark Brown { 77967921a1aSMarkus Pargmann if (regmap_can_raw_write(map) && !map->use_single_write) 7803f4ff561SLars-Peter Clausen return regcache_sync_block_raw(map, block, cache_present, 7813f4ff561SLars-Peter Clausen block_base, start, end); 782cfdeb8c3SMark Brown else 7833f4ff561SLars-Peter Clausen return regcache_sync_block_single(map, block, cache_present, 7843f4ff561SLars-Peter Clausen block_base, start, end); 785cfdeb8c3SMark Brown } 786