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, 24*f458e610SMark Brown #if IS_ENABLED(CONFIG_REGCACHE_COMPRESSED) 252cbbb579SDimitris Papastamos ®cache_lzo_ops, 2634a730aaSJonas Gorski #endif 272ac902ceSMark Brown ®cache_flat_ops, 289fabe24eSDimitris Papastamos }; 299fabe24eSDimitris Papastamos 309fabe24eSDimitris Papastamos static int regcache_hw_init(struct regmap *map) 319fabe24eSDimitris Papastamos { 329fabe24eSDimitris Papastamos int i, j; 339fabe24eSDimitris Papastamos int ret; 349fabe24eSDimitris Papastamos int count; 353245d460SMark Brown unsigned int reg, val; 369fabe24eSDimitris Papastamos void *tmp_buf; 379fabe24eSDimitris Papastamos 389fabe24eSDimitris Papastamos if (!map->num_reg_defaults_raw) 399fabe24eSDimitris Papastamos return -EINVAL; 409fabe24eSDimitris Papastamos 41fb70067eSXiubo Li /* calculate the size of reg_defaults */ 42fb70067eSXiubo Li for (count = 0, i = 0; i < map->num_reg_defaults_raw; i++) 43b2c7f5d9SMaarten ter Huurne if (regmap_readable(map, i * map->reg_stride) && 44b2c7f5d9SMaarten ter Huurne !regmap_volatile(map, i * map->reg_stride)) 45fb70067eSXiubo Li count++; 46fb70067eSXiubo Li 47b2c7f5d9SMaarten ter Huurne /* all registers are unreadable or volatile, so just bypass */ 48fb70067eSXiubo Li if (!count) { 49fb70067eSXiubo Li map->cache_bypass = true; 50fb70067eSXiubo Li return 0; 51fb70067eSXiubo Li } 52fb70067eSXiubo Li 53fb70067eSXiubo Li map->num_reg_defaults = count; 54fb70067eSXiubo Li map->reg_defaults = kmalloc_array(count, sizeof(struct reg_default), 55fb70067eSXiubo Li GFP_KERNEL); 56fb70067eSXiubo Li if (!map->reg_defaults) 57fb70067eSXiubo Li return -ENOMEM; 58fb70067eSXiubo Li 599fabe24eSDimitris Papastamos if (!map->reg_defaults_raw) { 60621a5f7aSViresh Kumar bool cache_bypass = map->cache_bypass; 619fabe24eSDimitris Papastamos dev_warn(map->dev, "No cache defaults, reading back from HW\n"); 62df00c79fSLaxman Dewangan 63df00c79fSLaxman Dewangan /* Bypass the cache access till data read from HW */ 64621a5f7aSViresh Kumar map->cache_bypass = true; 659fabe24eSDimitris Papastamos tmp_buf = kmalloc(map->cache_size_raw, GFP_KERNEL); 66fb70067eSXiubo Li if (!tmp_buf) { 67fb70067eSXiubo Li ret = -ENOMEM; 68fb70067eSXiubo Li goto err_free; 69fb70067eSXiubo Li } 70eb4cb76fSMark Brown ret = regmap_raw_read(map, 0, tmp_buf, 71d51fe1f3SMaciej S. Szmigiero map->cache_size_raw); 72df00c79fSLaxman Dewangan map->cache_bypass = cache_bypass; 733245d460SMark Brown if (ret == 0) { 749fabe24eSDimitris Papastamos map->reg_defaults_raw = tmp_buf; 759fabe24eSDimitris Papastamos map->cache_free = 1; 763245d460SMark Brown } else { 773245d460SMark Brown kfree(tmp_buf); 783245d460SMark Brown } 799fabe24eSDimitris Papastamos } 809fabe24eSDimitris Papastamos 819fabe24eSDimitris Papastamos /* fill the reg_defaults */ 829fabe24eSDimitris Papastamos for (i = 0, j = 0; i < map->num_reg_defaults_raw; i++) { 833245d460SMark Brown reg = i * map->reg_stride; 843245d460SMark Brown 853245d460SMark Brown if (!regmap_readable(map, reg)) 869fabe24eSDimitris Papastamos continue; 873245d460SMark Brown 883245d460SMark Brown if (regmap_volatile(map, reg)) 893245d460SMark Brown continue; 903245d460SMark Brown 913245d460SMark Brown if (map->reg_defaults_raw) { 92fbba43c5SXiubo Li val = regcache_get_val(map, map->reg_defaults_raw, i); 933245d460SMark Brown } else { 943245d460SMark Brown bool cache_bypass = map->cache_bypass; 953245d460SMark Brown 963245d460SMark Brown map->cache_bypass = true; 973245d460SMark Brown ret = regmap_read(map, reg, &val); 983245d460SMark Brown map->cache_bypass = cache_bypass; 993245d460SMark Brown if (ret != 0) { 1003245d460SMark Brown dev_err(map->dev, "Failed to read %d: %d\n", 1013245d460SMark Brown reg, ret); 1023245d460SMark Brown goto err_free; 1033245d460SMark Brown } 1043245d460SMark Brown } 1053245d460SMark Brown 1063245d460SMark Brown map->reg_defaults[j].reg = reg; 1079fabe24eSDimitris Papastamos map->reg_defaults[j].def = val; 1089fabe24eSDimitris Papastamos j++; 1099fabe24eSDimitris Papastamos } 1109fabe24eSDimitris Papastamos 1119fabe24eSDimitris Papastamos return 0; 112021cd616SLars-Peter Clausen 113021cd616SLars-Peter Clausen err_free: 114fb70067eSXiubo Li kfree(map->reg_defaults); 115021cd616SLars-Peter Clausen 116021cd616SLars-Peter Clausen return ret; 1179fabe24eSDimitris Papastamos } 1189fabe24eSDimitris Papastamos 119e5e3b8abSLars-Peter Clausen int regcache_init(struct regmap *map, const struct regmap_config *config) 1209fabe24eSDimitris Papastamos { 1219fabe24eSDimitris Papastamos int ret; 1229fabe24eSDimitris Papastamos int i; 1239fabe24eSDimitris Papastamos void *tmp_buf; 1249fabe24eSDimitris Papastamos 125e7a6db30SMark Brown if (map->cache_type == REGCACHE_NONE) { 1268cfe2fd3SXiubo Li if (config->reg_defaults || config->num_reg_defaults_raw) 1278cfe2fd3SXiubo Li dev_warn(map->dev, 1288cfe2fd3SXiubo Li "No cache used with register defaults set!\n"); 1298cfe2fd3SXiubo Li 130e7a6db30SMark Brown map->cache_bypass = true; 1319fabe24eSDimitris Papastamos return 0; 132e7a6db30SMark Brown } 1339fabe24eSDimitris Papastamos 134167f7066SXiubo Li if (config->reg_defaults && !config->num_reg_defaults) { 135167f7066SXiubo Li dev_err(map->dev, 136167f7066SXiubo Li "Register defaults are set without the number!\n"); 137167f7066SXiubo Li return -EINVAL; 138167f7066SXiubo Li } 139167f7066SXiubo Li 1408cfe2fd3SXiubo Li for (i = 0; i < config->num_reg_defaults; i++) 1418cfe2fd3SXiubo Li if (config->reg_defaults[i].reg % map->reg_stride) 1428cfe2fd3SXiubo Li return -EINVAL; 1438cfe2fd3SXiubo Li 1449fabe24eSDimitris Papastamos for (i = 0; i < ARRAY_SIZE(cache_types); i++) 1459fabe24eSDimitris Papastamos if (cache_types[i]->type == map->cache_type) 1469fabe24eSDimitris Papastamos break; 1479fabe24eSDimitris Papastamos 1489fabe24eSDimitris Papastamos if (i == ARRAY_SIZE(cache_types)) { 1499fabe24eSDimitris Papastamos dev_err(map->dev, "Could not match compress type: %d\n", 1509fabe24eSDimitris Papastamos map->cache_type); 1519fabe24eSDimitris Papastamos return -EINVAL; 1529fabe24eSDimitris Papastamos } 1539fabe24eSDimitris Papastamos 154e5e3b8abSLars-Peter Clausen map->num_reg_defaults = config->num_reg_defaults; 155e5e3b8abSLars-Peter Clausen map->num_reg_defaults_raw = config->num_reg_defaults_raw; 156e5e3b8abSLars-Peter Clausen map->reg_defaults_raw = config->reg_defaults_raw; 157064d4db1SLars-Peter Clausen map->cache_word_size = DIV_ROUND_UP(config->val_bits, 8); 158064d4db1SLars-Peter Clausen map->cache_size_raw = map->cache_word_size * config->num_reg_defaults_raw; 159e5e3b8abSLars-Peter Clausen 1609fabe24eSDimitris Papastamos map->cache = NULL; 1619fabe24eSDimitris Papastamos map->cache_ops = cache_types[i]; 1629fabe24eSDimitris Papastamos 1639fabe24eSDimitris Papastamos if (!map->cache_ops->read || 1649fabe24eSDimitris Papastamos !map->cache_ops->write || 1659fabe24eSDimitris Papastamos !map->cache_ops->name) 1669fabe24eSDimitris Papastamos return -EINVAL; 1679fabe24eSDimitris Papastamos 1689fabe24eSDimitris Papastamos /* We still need to ensure that the reg_defaults 1699fabe24eSDimitris Papastamos * won't vanish from under us. We'll need to make 1709fabe24eSDimitris Papastamos * a copy of it. 1719fabe24eSDimitris Papastamos */ 172720e4616SLars-Peter Clausen if (config->reg_defaults) { 173720e4616SLars-Peter Clausen tmp_buf = kmemdup(config->reg_defaults, map->num_reg_defaults * 1749fabe24eSDimitris Papastamos sizeof(struct reg_default), GFP_KERNEL); 1759fabe24eSDimitris Papastamos if (!tmp_buf) 1769fabe24eSDimitris Papastamos return -ENOMEM; 1779fabe24eSDimitris Papastamos map->reg_defaults = tmp_buf; 1788528bdd4SMark Brown } else if (map->num_reg_defaults_raw) { 1795fcd2560SMark Brown /* Some devices such as PMICs don't have cache defaults, 1809fabe24eSDimitris Papastamos * we cope with this by reading back the HW registers and 1819fabe24eSDimitris Papastamos * crafting the cache defaults by hand. 1829fabe24eSDimitris Papastamos */ 1839fabe24eSDimitris Papastamos ret = regcache_hw_init(map); 1849fabe24eSDimitris Papastamos if (ret < 0) 1859fabe24eSDimitris Papastamos return ret; 186fb70067eSXiubo Li if (map->cache_bypass) 187fb70067eSXiubo Li return 0; 1889fabe24eSDimitris Papastamos } 1899fabe24eSDimitris Papastamos 1909fabe24eSDimitris Papastamos if (!map->max_register) 1919fabe24eSDimitris Papastamos map->max_register = map->num_reg_defaults_raw; 1929fabe24eSDimitris Papastamos 1939fabe24eSDimitris Papastamos if (map->cache_ops->init) { 1949fabe24eSDimitris Papastamos dev_dbg(map->dev, "Initializing %s cache\n", 1959fabe24eSDimitris Papastamos map->cache_ops->name); 196bd061c78SLars-Peter Clausen ret = map->cache_ops->init(map); 197bd061c78SLars-Peter Clausen if (ret) 198bd061c78SLars-Peter Clausen goto err_free; 1999fabe24eSDimitris Papastamos } 2009fabe24eSDimitris Papastamos return 0; 201bd061c78SLars-Peter Clausen 202bd061c78SLars-Peter Clausen err_free: 203bd061c78SLars-Peter Clausen kfree(map->reg_defaults); 204bd061c78SLars-Peter Clausen if (map->cache_free) 205bd061c78SLars-Peter Clausen kfree(map->reg_defaults_raw); 206bd061c78SLars-Peter Clausen 207bd061c78SLars-Peter Clausen return ret; 2089fabe24eSDimitris Papastamos } 2099fabe24eSDimitris Papastamos 2109fabe24eSDimitris Papastamos void regcache_exit(struct regmap *map) 2119fabe24eSDimitris Papastamos { 2129fabe24eSDimitris Papastamos if (map->cache_type == REGCACHE_NONE) 2139fabe24eSDimitris Papastamos return; 2149fabe24eSDimitris Papastamos 2159fabe24eSDimitris Papastamos BUG_ON(!map->cache_ops); 2169fabe24eSDimitris Papastamos 2179fabe24eSDimitris Papastamos kfree(map->reg_defaults); 2189fabe24eSDimitris Papastamos if (map->cache_free) 2199fabe24eSDimitris Papastamos kfree(map->reg_defaults_raw); 2209fabe24eSDimitris Papastamos 2219fabe24eSDimitris Papastamos if (map->cache_ops->exit) { 2229fabe24eSDimitris Papastamos dev_dbg(map->dev, "Destroying %s cache\n", 2239fabe24eSDimitris Papastamos map->cache_ops->name); 2249fabe24eSDimitris Papastamos map->cache_ops->exit(map); 2259fabe24eSDimitris Papastamos } 2269fabe24eSDimitris Papastamos } 2279fabe24eSDimitris Papastamos 2289fabe24eSDimitris Papastamos /** 2292cf8e2dfSCharles Keepax * regcache_read - Fetch the value of a given register from the cache. 2309fabe24eSDimitris Papastamos * 2319fabe24eSDimitris Papastamos * @map: map to configure. 2329fabe24eSDimitris Papastamos * @reg: The register index. 2339fabe24eSDimitris Papastamos * @value: The value to be returned. 2349fabe24eSDimitris Papastamos * 2359fabe24eSDimitris Papastamos * Return a negative value on failure, 0 on success. 2369fabe24eSDimitris Papastamos */ 2379fabe24eSDimitris Papastamos int regcache_read(struct regmap *map, 2389fabe24eSDimitris Papastamos unsigned int reg, unsigned int *value) 2399fabe24eSDimitris Papastamos { 240bc7ee556SMark Brown int ret; 241bc7ee556SMark Brown 2429fabe24eSDimitris Papastamos if (map->cache_type == REGCACHE_NONE) 2439fabe24eSDimitris Papastamos return -ENOSYS; 2449fabe24eSDimitris Papastamos 2459fabe24eSDimitris Papastamos BUG_ON(!map->cache_ops); 2469fabe24eSDimitris Papastamos 247bc7ee556SMark Brown if (!regmap_volatile(map, reg)) { 248bc7ee556SMark Brown ret = map->cache_ops->read(map, reg, value); 249bc7ee556SMark Brown 250bc7ee556SMark Brown if (ret == 0) 251c6b570d9SPhilipp Zabel trace_regmap_reg_read_cache(map, reg, *value); 252bc7ee556SMark Brown 253bc7ee556SMark Brown return ret; 254bc7ee556SMark Brown } 2559fabe24eSDimitris Papastamos 2569fabe24eSDimitris Papastamos return -EINVAL; 2579fabe24eSDimitris Papastamos } 2589fabe24eSDimitris Papastamos 2599fabe24eSDimitris Papastamos /** 2602cf8e2dfSCharles Keepax * regcache_write - Set the value of a given register in the cache. 2619fabe24eSDimitris Papastamos * 2629fabe24eSDimitris Papastamos * @map: map to configure. 2639fabe24eSDimitris Papastamos * @reg: The register index. 2649fabe24eSDimitris Papastamos * @value: The new register value. 2659fabe24eSDimitris Papastamos * 2669fabe24eSDimitris Papastamos * Return a negative value on failure, 0 on success. 2679fabe24eSDimitris Papastamos */ 2689fabe24eSDimitris Papastamos int regcache_write(struct regmap *map, 2699fabe24eSDimitris Papastamos unsigned int reg, unsigned int value) 2709fabe24eSDimitris Papastamos { 2719fabe24eSDimitris Papastamos if (map->cache_type == REGCACHE_NONE) 2729fabe24eSDimitris Papastamos return 0; 2739fabe24eSDimitris Papastamos 2749fabe24eSDimitris Papastamos BUG_ON(!map->cache_ops); 2759fabe24eSDimitris Papastamos 2769fabe24eSDimitris Papastamos if (!regmap_volatile(map, reg)) 2779fabe24eSDimitris Papastamos return map->cache_ops->write(map, reg, value); 2789fabe24eSDimitris Papastamos 2799fabe24eSDimitris Papastamos return 0; 2809fabe24eSDimitris Papastamos } 2819fabe24eSDimitris Papastamos 2823969fa08SKevin Cernekee static bool regcache_reg_needs_sync(struct regmap *map, unsigned int reg, 2833969fa08SKevin Cernekee unsigned int val) 2843969fa08SKevin Cernekee { 2853969fa08SKevin Cernekee int ret; 2863969fa08SKevin Cernekee 2871c79771aSKevin Cernekee /* If we don't know the chip just got reset, then sync everything. */ 2881c79771aSKevin Cernekee if (!map->no_sync_defaults) 2891c79771aSKevin Cernekee return true; 2901c79771aSKevin Cernekee 2913969fa08SKevin Cernekee /* Is this the hardware default? If so skip. */ 2923969fa08SKevin Cernekee ret = regcache_lookup_reg(map, reg); 2933969fa08SKevin Cernekee if (ret >= 0 && val == map->reg_defaults[ret].def) 2943969fa08SKevin Cernekee return false; 2953969fa08SKevin Cernekee return true; 2963969fa08SKevin Cernekee } 2973969fa08SKevin Cernekee 298d856fce4SMaarten ter Huurne static int regcache_default_sync(struct regmap *map, unsigned int min, 299d856fce4SMaarten ter Huurne unsigned int max) 300d856fce4SMaarten ter Huurne { 301d856fce4SMaarten ter Huurne unsigned int reg; 302d856fce4SMaarten ter Huurne 30375617328SDylan Reid for (reg = min; reg <= max; reg += map->reg_stride) { 304d856fce4SMaarten ter Huurne unsigned int val; 305d856fce4SMaarten ter Huurne int ret; 306d856fce4SMaarten ter Huurne 30783f8475cSDylan Reid if (regmap_volatile(map, reg) || 30883f8475cSDylan Reid !regmap_writeable(map, reg)) 309d856fce4SMaarten ter Huurne continue; 310d856fce4SMaarten ter Huurne 311d856fce4SMaarten ter Huurne ret = regcache_read(map, reg, &val); 312d856fce4SMaarten ter Huurne if (ret) 313d856fce4SMaarten ter Huurne return ret; 314d856fce4SMaarten ter Huurne 3153969fa08SKevin Cernekee if (!regcache_reg_needs_sync(map, reg, val)) 316d856fce4SMaarten ter Huurne continue; 317d856fce4SMaarten ter Huurne 318621a5f7aSViresh Kumar map->cache_bypass = true; 319d856fce4SMaarten ter Huurne ret = _regmap_write(map, reg, val); 320621a5f7aSViresh Kumar map->cache_bypass = false; 321f29a4320SJarkko Nikula if (ret) { 322f29a4320SJarkko Nikula dev_err(map->dev, "Unable to sync register %#x. %d\n", 323f29a4320SJarkko Nikula reg, ret); 324d856fce4SMaarten ter Huurne return ret; 325f29a4320SJarkko Nikula } 326d856fce4SMaarten ter Huurne dev_dbg(map->dev, "Synced register %#x, value %#x\n", reg, val); 327d856fce4SMaarten ter Huurne } 328d856fce4SMaarten ter Huurne 329d856fce4SMaarten ter Huurne return 0; 330d856fce4SMaarten ter Huurne } 331d856fce4SMaarten ter Huurne 3329fabe24eSDimitris Papastamos /** 3332cf8e2dfSCharles Keepax * regcache_sync - Sync the register cache with the hardware. 3349fabe24eSDimitris Papastamos * 3359fabe24eSDimitris Papastamos * @map: map to configure. 3369fabe24eSDimitris Papastamos * 3379fabe24eSDimitris Papastamos * Any registers that should not be synced should be marked as 3389fabe24eSDimitris Papastamos * volatile. In general drivers can choose not to use the provided 3399fabe24eSDimitris Papastamos * syncing functionality if they so require. 3409fabe24eSDimitris Papastamos * 3419fabe24eSDimitris Papastamos * Return a negative value on failure, 0 on success. 3429fabe24eSDimitris Papastamos */ 3439fabe24eSDimitris Papastamos int regcache_sync(struct regmap *map) 3449fabe24eSDimitris Papastamos { 345954757d7SDimitris Papastamos int ret = 0; 346954757d7SDimitris Papastamos unsigned int i; 34759360089SDimitris Papastamos const char *name; 348621a5f7aSViresh Kumar bool bypass; 34959360089SDimitris Papastamos 350d856fce4SMaarten ter Huurne BUG_ON(!map->cache_ops); 3519fabe24eSDimitris Papastamos 35281485f52SLars-Peter Clausen map->lock(map->lock_arg); 353beb1a10fSDimitris Papastamos /* Remember the initial bypass state */ 354beb1a10fSDimitris Papastamos bypass = map->cache_bypass; 3559fabe24eSDimitris Papastamos dev_dbg(map->dev, "Syncing %s cache\n", 3569fabe24eSDimitris Papastamos map->cache_ops->name); 35759360089SDimitris Papastamos name = map->cache_ops->name; 358c6b570d9SPhilipp Zabel trace_regcache_sync(map, name, "start"); 35922f0d90aSMark Brown 3608ae0d7e8SMark Brown if (!map->cache_dirty) 3618ae0d7e8SMark Brown goto out; 362d9db7627SMark Brown 363affbe886SMark Brown map->async = true; 364affbe886SMark Brown 36522f0d90aSMark Brown /* Apply any patch first */ 366621a5f7aSViresh Kumar map->cache_bypass = true; 36722f0d90aSMark Brown for (i = 0; i < map->patch_regs; i++) { 36822f0d90aSMark Brown ret = _regmap_write(map, map->patch[i].reg, map->patch[i].def); 36922f0d90aSMark Brown if (ret != 0) { 37022f0d90aSMark Brown dev_err(map->dev, "Failed to write %x = %x: %d\n", 37122f0d90aSMark Brown map->patch[i].reg, map->patch[i].def, ret); 37222f0d90aSMark Brown goto out; 37322f0d90aSMark Brown } 37422f0d90aSMark Brown } 375621a5f7aSViresh Kumar map->cache_bypass = false; 37622f0d90aSMark Brown 377d856fce4SMaarten ter Huurne if (map->cache_ops->sync) 378ac8d91c8SMark Brown ret = map->cache_ops->sync(map, 0, map->max_register); 379d856fce4SMaarten ter Huurne else 380d856fce4SMaarten ter Huurne ret = regcache_default_sync(map, 0, map->max_register); 381954757d7SDimitris Papastamos 3826ff73738SMark Brown if (ret == 0) 3836ff73738SMark Brown map->cache_dirty = false; 3846ff73738SMark Brown 385954757d7SDimitris Papastamos out: 386beb1a10fSDimitris Papastamos /* Restore the bypass state */ 387affbe886SMark Brown map->async = false; 388beb1a10fSDimitris Papastamos map->cache_bypass = bypass; 3891c79771aSKevin Cernekee map->no_sync_defaults = false; 39081485f52SLars-Peter Clausen map->unlock(map->lock_arg); 391954757d7SDimitris Papastamos 392affbe886SMark Brown regmap_async_complete(map); 393affbe886SMark Brown 394c6b570d9SPhilipp Zabel trace_regcache_sync(map, name, "stop"); 395affbe886SMark Brown 396954757d7SDimitris Papastamos return ret; 3979fabe24eSDimitris Papastamos } 3989fabe24eSDimitris Papastamos EXPORT_SYMBOL_GPL(regcache_sync); 3999fabe24eSDimitris Papastamos 40092afb286SMark Brown /** 4012cf8e2dfSCharles Keepax * regcache_sync_region - Sync part of the register cache with the hardware. 4024d4cfd16SMark Brown * 4034d4cfd16SMark Brown * @map: map to sync. 4044d4cfd16SMark Brown * @min: first register to sync 4054d4cfd16SMark Brown * @max: last register to sync 4064d4cfd16SMark Brown * 4074d4cfd16SMark Brown * Write all non-default register values in the specified region to 4084d4cfd16SMark Brown * the hardware. 4094d4cfd16SMark Brown * 4104d4cfd16SMark Brown * Return a negative value on failure, 0 on success. 4114d4cfd16SMark Brown */ 4124d4cfd16SMark Brown int regcache_sync_region(struct regmap *map, unsigned int min, 4134d4cfd16SMark Brown unsigned int max) 4144d4cfd16SMark Brown { 4154d4cfd16SMark Brown int ret = 0; 4164d4cfd16SMark Brown const char *name; 417621a5f7aSViresh Kumar bool bypass; 4184d4cfd16SMark Brown 419d856fce4SMaarten ter Huurne BUG_ON(!map->cache_ops); 4204d4cfd16SMark Brown 42181485f52SLars-Peter Clausen map->lock(map->lock_arg); 4224d4cfd16SMark Brown 4234d4cfd16SMark Brown /* Remember the initial bypass state */ 4244d4cfd16SMark Brown bypass = map->cache_bypass; 4254d4cfd16SMark Brown 4264d4cfd16SMark Brown name = map->cache_ops->name; 4274d4cfd16SMark Brown dev_dbg(map->dev, "Syncing %s cache from %d-%d\n", name, min, max); 4284d4cfd16SMark Brown 429c6b570d9SPhilipp Zabel trace_regcache_sync(map, name, "start region"); 4304d4cfd16SMark Brown 4314d4cfd16SMark Brown if (!map->cache_dirty) 4324d4cfd16SMark Brown goto out; 4334d4cfd16SMark Brown 434affbe886SMark Brown map->async = true; 435affbe886SMark Brown 436d856fce4SMaarten ter Huurne if (map->cache_ops->sync) 4374d4cfd16SMark Brown ret = map->cache_ops->sync(map, min, max); 438d856fce4SMaarten ter Huurne else 439d856fce4SMaarten ter Huurne ret = regcache_default_sync(map, min, max); 4404d4cfd16SMark Brown 4414d4cfd16SMark Brown out: 4424d4cfd16SMark Brown /* Restore the bypass state */ 4434d4cfd16SMark Brown map->cache_bypass = bypass; 444affbe886SMark Brown map->async = false; 4451c79771aSKevin Cernekee map->no_sync_defaults = false; 44681485f52SLars-Peter Clausen map->unlock(map->lock_arg); 4474d4cfd16SMark Brown 448affbe886SMark Brown regmap_async_complete(map); 449affbe886SMark Brown 450c6b570d9SPhilipp Zabel trace_regcache_sync(map, name, "stop region"); 451affbe886SMark Brown 4524d4cfd16SMark Brown return ret; 4534d4cfd16SMark Brown } 454e466de05SMark Brown EXPORT_SYMBOL_GPL(regcache_sync_region); 4554d4cfd16SMark Brown 4564d4cfd16SMark Brown /** 4572cf8e2dfSCharles Keepax * regcache_drop_region - Discard part of the register cache 458697e85bcSMark Brown * 459697e85bcSMark Brown * @map: map to operate on 460697e85bcSMark Brown * @min: first register to discard 461697e85bcSMark Brown * @max: last register to discard 462697e85bcSMark Brown * 463697e85bcSMark Brown * Discard part of the register cache. 464697e85bcSMark Brown * 465697e85bcSMark Brown * Return a negative value on failure, 0 on success. 466697e85bcSMark Brown */ 467697e85bcSMark Brown int regcache_drop_region(struct regmap *map, unsigned int min, 468697e85bcSMark Brown unsigned int max) 469697e85bcSMark Brown { 470697e85bcSMark Brown int ret = 0; 471697e85bcSMark Brown 4723f4ff561SLars-Peter Clausen if (!map->cache_ops || !map->cache_ops->drop) 473697e85bcSMark Brown return -EINVAL; 474697e85bcSMark Brown 47581485f52SLars-Peter Clausen map->lock(map->lock_arg); 476697e85bcSMark Brown 477c6b570d9SPhilipp Zabel trace_regcache_drop_region(map, min, max); 478697e85bcSMark Brown 479697e85bcSMark Brown ret = map->cache_ops->drop(map, min, max); 480697e85bcSMark Brown 48181485f52SLars-Peter Clausen map->unlock(map->lock_arg); 482697e85bcSMark Brown 483697e85bcSMark Brown return ret; 484697e85bcSMark Brown } 485697e85bcSMark Brown EXPORT_SYMBOL_GPL(regcache_drop_region); 486697e85bcSMark Brown 487697e85bcSMark Brown /** 4882cf8e2dfSCharles Keepax * regcache_cache_only - Put a register map into cache only mode 48992afb286SMark Brown * 49092afb286SMark Brown * @map: map to configure 4912cf8e2dfSCharles Keepax * @enable: flag if changes should be written to the hardware 49292afb286SMark Brown * 49392afb286SMark Brown * When a register map is marked as cache only writes to the register 49492afb286SMark Brown * map API will only update the register cache, they will not cause 49592afb286SMark Brown * any hardware changes. This is useful for allowing portions of 49692afb286SMark Brown * drivers to act as though the device were functioning as normal when 49792afb286SMark Brown * it is disabled for power saving reasons. 49892afb286SMark Brown */ 49992afb286SMark Brown void regcache_cache_only(struct regmap *map, bool enable) 50092afb286SMark Brown { 50181485f52SLars-Peter Clausen map->lock(map->lock_arg); 502ac77a765SDimitris Papastamos WARN_ON(map->cache_bypass && enable); 50392afb286SMark Brown map->cache_only = enable; 504c6b570d9SPhilipp Zabel trace_regmap_cache_only(map, enable); 50581485f52SLars-Peter Clausen map->unlock(map->lock_arg); 50692afb286SMark Brown } 50792afb286SMark Brown EXPORT_SYMBOL_GPL(regcache_cache_only); 50892afb286SMark Brown 5096eb0f5e0SDimitris Papastamos /** 5102cf8e2dfSCharles Keepax * regcache_mark_dirty - Indicate that HW registers were reset to default values 5118ae0d7e8SMark Brown * 5128ae0d7e8SMark Brown * @map: map to mark 5138ae0d7e8SMark Brown * 5141c79771aSKevin Cernekee * Inform regcache that the device has been powered down or reset, so that 5151c79771aSKevin Cernekee * on resume, regcache_sync() knows to write out all non-default values 5161c79771aSKevin Cernekee * stored in the cache. 5171c79771aSKevin Cernekee * 5181c79771aSKevin Cernekee * If this function is not called, regcache_sync() will assume that 5191c79771aSKevin Cernekee * the hardware state still matches the cache state, modulo any writes that 5201c79771aSKevin Cernekee * happened when cache_only was true. 5218ae0d7e8SMark Brown */ 5228ae0d7e8SMark Brown void regcache_mark_dirty(struct regmap *map) 5238ae0d7e8SMark Brown { 52481485f52SLars-Peter Clausen map->lock(map->lock_arg); 5258ae0d7e8SMark Brown map->cache_dirty = true; 5261c79771aSKevin Cernekee map->no_sync_defaults = true; 52781485f52SLars-Peter Clausen map->unlock(map->lock_arg); 5288ae0d7e8SMark Brown } 5298ae0d7e8SMark Brown EXPORT_SYMBOL_GPL(regcache_mark_dirty); 5308ae0d7e8SMark Brown 5318ae0d7e8SMark Brown /** 5322cf8e2dfSCharles Keepax * regcache_cache_bypass - Put a register map into cache bypass mode 5336eb0f5e0SDimitris Papastamos * 5346eb0f5e0SDimitris Papastamos * @map: map to configure 5352cf8e2dfSCharles Keepax * @enable: flag if changes should not be written to the cache 5366eb0f5e0SDimitris Papastamos * 5376eb0f5e0SDimitris Papastamos * When a register map is marked with the cache bypass option, writes 5386eb0f5e0SDimitris Papastamos * to the register map API will only update the hardware and not the 5396eb0f5e0SDimitris Papastamos * the cache directly. This is useful when syncing the cache back to 5406eb0f5e0SDimitris Papastamos * the hardware. 5416eb0f5e0SDimitris Papastamos */ 5426eb0f5e0SDimitris Papastamos void regcache_cache_bypass(struct regmap *map, bool enable) 5436eb0f5e0SDimitris Papastamos { 54481485f52SLars-Peter Clausen map->lock(map->lock_arg); 545ac77a765SDimitris Papastamos WARN_ON(map->cache_only && enable); 5466eb0f5e0SDimitris Papastamos map->cache_bypass = enable; 547c6b570d9SPhilipp Zabel trace_regmap_cache_bypass(map, enable); 54881485f52SLars-Peter Clausen map->unlock(map->lock_arg); 5496eb0f5e0SDimitris Papastamos } 5506eb0f5e0SDimitris Papastamos EXPORT_SYMBOL_GPL(regcache_cache_bypass); 5516eb0f5e0SDimitris Papastamos 552879082c9SMark Brown bool regcache_set_val(struct regmap *map, void *base, unsigned int idx, 553879082c9SMark Brown unsigned int val) 5549fabe24eSDimitris Papastamos { 555325acab4SMark Brown if (regcache_get_val(map, base, idx) == val) 556325acab4SMark Brown return true; 557325acab4SMark Brown 558eb4cb76fSMark Brown /* Use device native format if possible */ 559eb4cb76fSMark Brown if (map->format.format_val) { 560eb4cb76fSMark Brown map->format.format_val(base + (map->cache_word_size * idx), 561eb4cb76fSMark Brown val, 0); 562eb4cb76fSMark Brown return false; 563eb4cb76fSMark Brown } 564eb4cb76fSMark Brown 565879082c9SMark Brown switch (map->cache_word_size) { 5669fabe24eSDimitris Papastamos case 1: { 5679fabe24eSDimitris Papastamos u8 *cache = base; 5682fd6902eSXiubo Li 5699fabe24eSDimitris Papastamos cache[idx] = val; 5709fabe24eSDimitris Papastamos break; 5719fabe24eSDimitris Papastamos } 5729fabe24eSDimitris Papastamos case 2: { 5739fabe24eSDimitris Papastamos u16 *cache = base; 5742fd6902eSXiubo Li 5759fabe24eSDimitris Papastamos cache[idx] = val; 5769fabe24eSDimitris Papastamos break; 5779fabe24eSDimitris Papastamos } 5787d5e525bSMark Brown case 4: { 5797d5e525bSMark Brown u32 *cache = base; 5802fd6902eSXiubo Li 5817d5e525bSMark Brown cache[idx] = val; 5827d5e525bSMark Brown break; 5837d5e525bSMark Brown } 5848b7663deSXiubo Li #ifdef CONFIG_64BIT 5858b7663deSXiubo Li case 8: { 5868b7663deSXiubo Li u64 *cache = base; 5878b7663deSXiubo Li 5888b7663deSXiubo Li cache[idx] = val; 5898b7663deSXiubo Li break; 5908b7663deSXiubo Li } 5918b7663deSXiubo Li #endif 5929fabe24eSDimitris Papastamos default: 5939fabe24eSDimitris Papastamos BUG(); 5949fabe24eSDimitris Papastamos } 5959fabe24eSDimitris Papastamos return false; 5969fabe24eSDimitris Papastamos } 5979fabe24eSDimitris Papastamos 598879082c9SMark Brown unsigned int regcache_get_val(struct regmap *map, const void *base, 599879082c9SMark Brown unsigned int idx) 6009fabe24eSDimitris Papastamos { 6019fabe24eSDimitris Papastamos if (!base) 6029fabe24eSDimitris Papastamos return -EINVAL; 6039fabe24eSDimitris Papastamos 604eb4cb76fSMark Brown /* Use device native format if possible */ 605eb4cb76fSMark Brown if (map->format.parse_val) 6068817796bSMark Brown return map->format.parse_val(regcache_get_val_addr(map, base, 6078817796bSMark Brown idx)); 608eb4cb76fSMark Brown 609879082c9SMark Brown switch (map->cache_word_size) { 6109fabe24eSDimitris Papastamos case 1: { 6119fabe24eSDimitris Papastamos const u8 *cache = base; 6122fd6902eSXiubo Li 6139fabe24eSDimitris Papastamos return cache[idx]; 6149fabe24eSDimitris Papastamos } 6159fabe24eSDimitris Papastamos case 2: { 6169fabe24eSDimitris Papastamos const u16 *cache = base; 6172fd6902eSXiubo Li 6189fabe24eSDimitris Papastamos return cache[idx]; 6199fabe24eSDimitris Papastamos } 6207d5e525bSMark Brown case 4: { 6217d5e525bSMark Brown const u32 *cache = base; 6222fd6902eSXiubo Li 6237d5e525bSMark Brown return cache[idx]; 6247d5e525bSMark Brown } 6258b7663deSXiubo Li #ifdef CONFIG_64BIT 6268b7663deSXiubo Li case 8: { 6278b7663deSXiubo Li const u64 *cache = base; 6288b7663deSXiubo Li 6298b7663deSXiubo Li return cache[idx]; 6308b7663deSXiubo Li } 6318b7663deSXiubo Li #endif 6329fabe24eSDimitris Papastamos default: 6339fabe24eSDimitris Papastamos BUG(); 6349fabe24eSDimitris Papastamos } 6359fabe24eSDimitris Papastamos /* unreachable */ 6369fabe24eSDimitris Papastamos return -1; 6379fabe24eSDimitris Papastamos } 6389fabe24eSDimitris Papastamos 639f094fea6SMark Brown static int regcache_default_cmp(const void *a, const void *b) 640c08604b8SDimitris Papastamos { 641c08604b8SDimitris Papastamos const struct reg_default *_a = a; 642c08604b8SDimitris Papastamos const struct reg_default *_b = b; 643c08604b8SDimitris Papastamos 644c08604b8SDimitris Papastamos return _a->reg - _b->reg; 645c08604b8SDimitris Papastamos } 646c08604b8SDimitris Papastamos 647f094fea6SMark Brown int regcache_lookup_reg(struct regmap *map, unsigned int reg) 648f094fea6SMark Brown { 649f094fea6SMark Brown struct reg_default key; 650f094fea6SMark Brown struct reg_default *r; 651f094fea6SMark Brown 652f094fea6SMark Brown key.reg = reg; 653f094fea6SMark Brown key.def = 0; 654f094fea6SMark Brown 655f094fea6SMark Brown r = bsearch(&key, map->reg_defaults, map->num_reg_defaults, 656f094fea6SMark Brown sizeof(struct reg_default), regcache_default_cmp); 657f094fea6SMark Brown 658f094fea6SMark Brown if (r) 659f094fea6SMark Brown return r - map->reg_defaults; 660f094fea6SMark Brown else 6616e6ace00SMark Brown return -ENOENT; 662f094fea6SMark Brown } 663f8bd822cSMark Brown 6643f4ff561SLars-Peter Clausen static bool regcache_reg_present(unsigned long *cache_present, unsigned int idx) 6653f4ff561SLars-Peter Clausen { 6663f4ff561SLars-Peter Clausen if (!cache_present) 6673f4ff561SLars-Peter Clausen return true; 6683f4ff561SLars-Peter Clausen 6693f4ff561SLars-Peter Clausen return test_bit(idx, cache_present); 6703f4ff561SLars-Peter Clausen } 6713f4ff561SLars-Peter Clausen 672cfdeb8c3SMark Brown static int regcache_sync_block_single(struct regmap *map, void *block, 6733f4ff561SLars-Peter Clausen unsigned long *cache_present, 674cfdeb8c3SMark Brown unsigned int block_base, 675cfdeb8c3SMark Brown unsigned int start, unsigned int end) 676cfdeb8c3SMark Brown { 677cfdeb8c3SMark Brown unsigned int i, regtmp, val; 678cfdeb8c3SMark Brown int ret; 679cfdeb8c3SMark Brown 680cfdeb8c3SMark Brown for (i = start; i < end; i++) { 681cfdeb8c3SMark Brown regtmp = block_base + (i * map->reg_stride); 682cfdeb8c3SMark Brown 6834ceba98dSTakashi Iwai if (!regcache_reg_present(cache_present, i) || 6844ceba98dSTakashi Iwai !regmap_writeable(map, regtmp)) 685cfdeb8c3SMark Brown continue; 686cfdeb8c3SMark Brown 687cfdeb8c3SMark Brown val = regcache_get_val(map, block, i); 6883969fa08SKevin Cernekee if (!regcache_reg_needs_sync(map, regtmp, val)) 689cfdeb8c3SMark Brown continue; 690cfdeb8c3SMark Brown 691621a5f7aSViresh Kumar map->cache_bypass = true; 692cfdeb8c3SMark Brown 693cfdeb8c3SMark Brown ret = _regmap_write(map, regtmp, val); 694cfdeb8c3SMark Brown 695621a5f7aSViresh Kumar map->cache_bypass = false; 696f29a4320SJarkko Nikula if (ret != 0) { 697f29a4320SJarkko Nikula dev_err(map->dev, "Unable to sync register %#x. %d\n", 698f29a4320SJarkko Nikula regtmp, ret); 699cfdeb8c3SMark Brown return ret; 700f29a4320SJarkko Nikula } 701cfdeb8c3SMark Brown dev_dbg(map->dev, "Synced register %#x, value %#x\n", 702cfdeb8c3SMark Brown regtmp, val); 703cfdeb8c3SMark Brown } 704cfdeb8c3SMark Brown 705cfdeb8c3SMark Brown return 0; 706cfdeb8c3SMark Brown } 707cfdeb8c3SMark Brown 70875a5f89fSMark Brown static int regcache_sync_block_raw_flush(struct regmap *map, const void **data, 70975a5f89fSMark Brown unsigned int base, unsigned int cur) 71075a5f89fSMark Brown { 71175a5f89fSMark Brown size_t val_bytes = map->format.val_bytes; 71275a5f89fSMark Brown int ret, count; 71375a5f89fSMark Brown 71475a5f89fSMark Brown if (*data == NULL) 71575a5f89fSMark Brown return 0; 71675a5f89fSMark Brown 71778ba73eeSDylan Reid count = (cur - base) / map->reg_stride; 71875a5f89fSMark Brown 7199659293cSStratos Karafotis dev_dbg(map->dev, "Writing %zu bytes for %d registers from 0x%x-0x%x\n", 72078ba73eeSDylan Reid count * val_bytes, count, base, cur - map->reg_stride); 72175a5f89fSMark Brown 722621a5f7aSViresh Kumar map->cache_bypass = true; 72375a5f89fSMark Brown 7240a819809SMark Brown ret = _regmap_raw_write(map, base, *data, count * val_bytes); 725f29a4320SJarkko Nikula if (ret) 726f29a4320SJarkko Nikula dev_err(map->dev, "Unable to sync registers %#x-%#x. %d\n", 727f29a4320SJarkko Nikula base, cur - map->reg_stride, ret); 72875a5f89fSMark Brown 729621a5f7aSViresh Kumar map->cache_bypass = false; 73075a5f89fSMark Brown 73175a5f89fSMark Brown *data = NULL; 73275a5f89fSMark Brown 73375a5f89fSMark Brown return ret; 73475a5f89fSMark Brown } 73575a5f89fSMark Brown 736f52687afSSachin Kamat static int regcache_sync_block_raw(struct regmap *map, void *block, 7373f4ff561SLars-Peter Clausen unsigned long *cache_present, 738f8bd822cSMark Brown unsigned int block_base, unsigned int start, 739f8bd822cSMark Brown unsigned int end) 740f8bd822cSMark Brown { 74175a5f89fSMark Brown unsigned int i, val; 74275a5f89fSMark Brown unsigned int regtmp = 0; 74375a5f89fSMark Brown unsigned int base = 0; 74475a5f89fSMark Brown const void *data = NULL; 745f8bd822cSMark Brown int ret; 746f8bd822cSMark Brown 747f8bd822cSMark Brown for (i = start; i < end; i++) { 748f8bd822cSMark Brown regtmp = block_base + (i * map->reg_stride); 749f8bd822cSMark Brown 7504ceba98dSTakashi Iwai if (!regcache_reg_present(cache_present, i) || 7514ceba98dSTakashi Iwai !regmap_writeable(map, regtmp)) { 75275a5f89fSMark Brown ret = regcache_sync_block_raw_flush(map, &data, 75375a5f89fSMark Brown base, regtmp); 75475a5f89fSMark Brown if (ret != 0) 75575a5f89fSMark Brown return ret; 756f8bd822cSMark Brown continue; 75775a5f89fSMark Brown } 758f8bd822cSMark Brown 759f8bd822cSMark Brown val = regcache_get_val(map, block, i); 7603969fa08SKevin Cernekee if (!regcache_reg_needs_sync(map, regtmp, val)) { 76175a5f89fSMark Brown ret = regcache_sync_block_raw_flush(map, &data, 76275a5f89fSMark Brown base, regtmp); 763f8bd822cSMark Brown if (ret != 0) 764f8bd822cSMark Brown return ret; 76575a5f89fSMark Brown continue; 766f8bd822cSMark Brown } 767f8bd822cSMark Brown 76875a5f89fSMark Brown if (!data) { 76975a5f89fSMark Brown data = regcache_get_val_addr(map, block, i); 77075a5f89fSMark Brown base = regtmp; 77175a5f89fSMark Brown } 77275a5f89fSMark Brown } 77375a5f89fSMark Brown 7742d49b598SLars-Peter Clausen return regcache_sync_block_raw_flush(map, &data, base, regtmp + 7752d49b598SLars-Peter Clausen map->reg_stride); 776f8bd822cSMark Brown } 777cfdeb8c3SMark Brown 778cfdeb8c3SMark Brown int regcache_sync_block(struct regmap *map, void *block, 7793f4ff561SLars-Peter Clausen unsigned long *cache_present, 780cfdeb8c3SMark Brown unsigned int block_base, unsigned int start, 781cfdeb8c3SMark Brown unsigned int end) 782cfdeb8c3SMark Brown { 78367921a1aSMarkus Pargmann if (regmap_can_raw_write(map) && !map->use_single_write) 7843f4ff561SLars-Peter Clausen return regcache_sync_block_raw(map, block, cache_present, 7853f4ff561SLars-Peter Clausen block_base, start, end); 786cfdeb8c3SMark Brown else 7873f4ff561SLars-Peter Clausen return regcache_sync_block_single(map, block, cache_present, 7883f4ff561SLars-Peter Clausen block_base, start, end); 789cfdeb8c3SMark Brown } 790