regmap-ram.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 = kzalloc_objs(bool, config->max_register + 1);
  56. if (!data->read)
  57. return ERR_PTR(-ENOMEM);
  58. data->written = kzalloc_objs(bool, config->max_register + 1);
  59. if (!data->written)
  60. return ERR_PTR(-ENOMEM);
  61. map = __regmap_init(dev, &regmap_ram, data, config,
  62. lock_key, lock_name);
  63. return map;
  64. }
  65. EXPORT_SYMBOL_GPL(__regmap_init_ram);
  66. MODULE_DESCRIPTION("Register map access API - Memory region");
  67. MODULE_LICENSE("GPL v2");