Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 //
0003 // Register cache access API - flat caching support
0004 //
0005 // Copyright 2012 Wolfson Microelectronics plc
0006 //
0007 // Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
0008 
0009 #include <linux/device.h>
0010 #include <linux/seq_file.h>
0011 #include <linux/slab.h>
0012 
0013 #include "internal.h"
0014 
0015 static inline unsigned int regcache_flat_get_index(const struct regmap *map,
0016                            unsigned int reg)
0017 {
0018     return regcache_get_index_by_order(map, reg);
0019 }
0020 
0021 static int regcache_flat_init(struct regmap *map)
0022 {
0023     int i;
0024     unsigned int *cache;
0025 
0026     if (!map || map->reg_stride_order < 0 || !map->max_register)
0027         return -EINVAL;
0028 
0029     map->cache = kcalloc(regcache_flat_get_index(map, map->max_register)
0030                  + 1, sizeof(unsigned int), GFP_KERNEL);
0031     if (!map->cache)
0032         return -ENOMEM;
0033 
0034     cache = map->cache;
0035 
0036     for (i = 0; i < map->num_reg_defaults; i++) {
0037         unsigned int reg = map->reg_defaults[i].reg;
0038         unsigned int index = regcache_flat_get_index(map, reg);
0039 
0040         cache[index] = map->reg_defaults[i].def;
0041     }
0042 
0043     return 0;
0044 }
0045 
0046 static int regcache_flat_exit(struct regmap *map)
0047 {
0048     kfree(map->cache);
0049     map->cache = NULL;
0050 
0051     return 0;
0052 }
0053 
0054 static int regcache_flat_read(struct regmap *map,
0055                   unsigned int reg, unsigned int *value)
0056 {
0057     unsigned int *cache = map->cache;
0058     unsigned int index = regcache_flat_get_index(map, reg);
0059 
0060     *value = cache[index];
0061 
0062     return 0;
0063 }
0064 
0065 static int regcache_flat_write(struct regmap *map, unsigned int reg,
0066                    unsigned int value)
0067 {
0068     unsigned int *cache = map->cache;
0069     unsigned int index = regcache_flat_get_index(map, reg);
0070 
0071     cache[index] = value;
0072 
0073     return 0;
0074 }
0075 
0076 struct regcache_ops regcache_flat_ops = {
0077     .type = REGCACHE_FLAT,
0078     .name = "flat",
0079     .init = regcache_flat_init,
0080     .exit = regcache_flat_exit,
0081     .read = regcache_flat_read,
0082     .write = regcache_flat_write,
0083 };