regcache-flat.c 1.9 KB

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