regcache-flat.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. cache[regcache_flat_get_index(map, map->reg_defaults[i].reg)] =
  34. map->reg_defaults[i].def;
  35. return 0;
  36. }
  37. static int regcache_flat_exit(struct regmap *map)
  38. {
  39. kfree(map->cache);
  40. map->cache = NULL;
  41. return 0;
  42. }
  43. static int regcache_flat_read(struct regmap *map,
  44. unsigned int reg, unsigned int *value)
  45. {
  46. unsigned int *cache = map->cache;
  47. *value = cache[regcache_flat_get_index(map, reg)];
  48. return 0;
  49. }
  50. static int regcache_flat_write(struct regmap *map, unsigned int reg,
  51. unsigned int value)
  52. {
  53. unsigned int *cache = map->cache;
  54. cache[regcache_flat_get_index(map, reg)] = 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. };