regmap-ram.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // Register map access API - Memory region
  4. //
  5. // This is intended for testing only
  6. //
  7. // Copyright (c) 2023, Arm Ltd
  8. #include <linux/clk.h>
  9. #include <linux/err.h>
  10. #include <linux/io.h>
  11. #include <linux/module.h>
  12. #include <linux/regmap.h>
  13. #include <linux/slab.h>
  14. #include <linux/swab.h>
  15. #include "internal.h"
  16. static int regmap_ram_write(void *context, unsigned int reg, unsigned int val)
  17. {
  18. struct regmap_ram_data *data = context;
  19. data->vals[reg] = val;
  20. data->written[reg] = true;
  21. return 0;
  22. }
  23. static int regmap_ram_read(void *context, unsigned int reg, unsigned int *val)
  24. {
  25. struct regmap_ram_data *data = context;
  26. *val = data->vals[reg];
  27. data->read[reg] = true;
  28. return 0;
  29. }
  30. static void regmap_ram_free_context(void *context)
  31. {
  32. struct regmap_ram_data *data = context;
  33. kfree(data->vals);
  34. kfree(data->read);
  35. kfree(data->written);
  36. kfree(data);
  37. }
  38. static const struct regmap_bus regmap_ram = {
  39. .fast_io = true,
  40. .reg_write = regmap_ram_write,
  41. .reg_read = regmap_ram_read,
  42. .free_context = regmap_ram_free_context,
  43. };
  44. struct regmap *__regmap_init_ram(struct device *dev,
  45. const struct regmap_config *config,
  46. struct regmap_ram_data *data,
  47. struct lock_class_key *lock_key,
  48. const char *lock_name)
  49. {
  50. struct regmap *map;
  51. if (!config->max_register) {
  52. pr_crit("No max_register specified for RAM regmap\n");
  53. return ERR_PTR(-EINVAL);
  54. }
  55. data->read = kcalloc(config->max_register + 1, sizeof(bool),
  56. GFP_KERNEL);
  57. if (!data->read)
  58. return ERR_PTR(-ENOMEM);
  59. data->written = kcalloc(config->max_register + 1, sizeof(bool),
  60. GFP_KERNEL);
  61. if (!data->written)
  62. return ERR_PTR(-ENOMEM);
  63. map = __regmap_init(dev, &regmap_ram, data, config,
  64. lock_key, lock_name);
  65. return map;
  66. }
  67. EXPORT_SYMBOL_GPL(__regmap_init_ram);
  68. MODULE_DESCRIPTION("Register map access API - Memory region");
  69. MODULE_LICENSE("GPL v2");