regcache-flat.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // Register cache access API - flat caching support
  4. //
  5. // Copyright 2012 Wolfson Microelectronics plc
  6. //
  7. // Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
  8. #include <linux/device.h>
  9. #include <linux/seq_file.h>
  10. #include <linux/slab.h>
  11. #include "internal.h"
  12. static inline unsigned int regcache_flat_get_index(const struct regmap *map,
  13. unsigned int reg)
  14. {
  15. return regcache_get_index_by_order(map, reg);
  16. }
  17. static int regcache_flat_init(struct regmap *map)
  18. {
  19. int i;
  20. unsigned int *cache;
  21. if (!map || map->reg_stride_order < 0 || !map->max_register)
  22. return -EINVAL;
  23. map->cache = kcalloc(regcache_flat_get_index(map, map->max_register)
  24. + 1, sizeof(unsigned int), GFP_KERNEL);
  25. if (!map->cache)
  26. return -ENOMEM;
  27. cache = map->cache;
  28. for (i = 0; i < map->num_reg_defaults; i++) {
  29. unsigned int reg = map->reg_defaults[i].reg;
  30. unsigned int index = regcache_flat_get_index(map, reg);
  31. cache[index] = map->reg_defaults[i].def;
  32. }
  33. return 0;
  34. }
  35. static int regcache_flat_exit(struct regmap *map)
  36. {
  37. kfree(map->cache);
  38. map->cache = NULL;
  39. return 0;
  40. }
  41. static int regcache_flat_read(struct regmap *map,
  42. unsigned int reg, unsigned int *value)
  43. {
  44. unsigned int *cache = map->cache;
  45. unsigned int index = regcache_flat_get_index(map, reg);
  46. *value = cache[index];
  47. return 0;
  48. }
  49. static int regcache_flat_write(struct regmap *map, unsigned int reg,
  50. unsigned int value)
  51. {
  52. unsigned int *cache = map->cache;
  53. unsigned int index = regcache_flat_get_index(map, reg);
  54. cache[index] = value;
  55. return 0;
  56. }
  57. struct regcache_ops regcache_flat_ops = {
  58. .type = REGCACHE_FLAT,
  59. .name = "flat",
  60. .init = regcache_flat_init,
  61. .exit = regcache_flat_exit,
  62. .read = regcache_flat_read,
  63. .write = regcache_flat_write,
  64. };