syscon-uclass.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2015 Google, Inc
  4. * Written by Simon Glass <sjg@chromium.org>
  5. */
  6. #include <common.h>
  7. #include <syscon.h>
  8. #include <dm.h>
  9. #include <errno.h>
  10. #include <regmap.h>
  11. #include <dm/device-internal.h>
  12. #include <dm/lists.h>
  13. #include <dm/root.h>
  14. #include <linux/err.h>
  15. /*
  16. * Caution:
  17. * This API requires the given device has alerady been bound to syscon driver.
  18. * For example,
  19. * compatible = "syscon", "simple-mfd";
  20. * works, but
  21. * compatible = "simple-mfd", "syscon";
  22. * does not. The behavior is different from Linux.
  23. */
  24. struct regmap *syscon_get_regmap(struct udevice *dev)
  25. {
  26. struct syscon_uc_info *priv;
  27. if (device_get_uclass_id(dev) != UCLASS_SYSCON)
  28. return ERR_PTR(-ENOEXEC);
  29. priv = dev_get_uclass_priv(dev);
  30. return priv->regmap;
  31. }
  32. static int syscon_pre_probe(struct udevice *dev)
  33. {
  34. struct syscon_uc_info *priv = dev_get_uclass_priv(dev);
  35. /*
  36. * With OF_PLATDATA we really have no way of knowing the format of
  37. * the device-specific platform data. So we assume that it starts with
  38. * a 'reg' member, and this holds a single address and size. Drivers
  39. * using OF_PLATDATA will need to ensure that this is true.
  40. */
  41. #if CONFIG_IS_ENABLED(OF_PLATDATA)
  42. struct syscon_base_platdata *plat = dev_get_platdata(dev);
  43. return regmap_init_mem_platdata(dev, plat->reg, ARRAY_SIZE(plat->reg),
  44. &priv->regmap);
  45. #else
  46. return regmap_init_mem(dev_ofnode(dev), &priv->regmap);
  47. #endif
  48. }
  49. int syscon_get_by_driver_data(ulong driver_data, struct udevice **devp)
  50. {
  51. struct udevice *dev;
  52. struct uclass *uc;
  53. int ret;
  54. *devp = NULL;
  55. ret = uclass_get(UCLASS_SYSCON, &uc);
  56. if (ret)
  57. return ret;
  58. uclass_foreach_dev(dev, uc) {
  59. if (dev->driver_data == driver_data) {
  60. *devp = dev;
  61. return device_probe(dev);
  62. }
  63. }
  64. return -ENODEV;
  65. }
  66. struct regmap *syscon_get_regmap_by_driver_data(ulong driver_data)
  67. {
  68. struct syscon_uc_info *priv;
  69. struct udevice *dev;
  70. int ret;
  71. ret = syscon_get_by_driver_data(driver_data, &dev);
  72. if (ret)
  73. return ERR_PTR(ret);
  74. priv = dev_get_uclass_priv(dev);
  75. return priv->regmap;
  76. }
  77. void *syscon_get_first_range(ulong driver_data)
  78. {
  79. struct regmap *map;
  80. map = syscon_get_regmap_by_driver_data(driver_data);
  81. if (IS_ERR(map))
  82. return map;
  83. return regmap_get_range(map, 0);
  84. }
  85. UCLASS_DRIVER(syscon) = {
  86. .id = UCLASS_SYSCON,
  87. .name = "syscon",
  88. .per_device_auto_alloc_size = sizeof(struct syscon_uc_info),
  89. .pre_probe = syscon_pre_probe,
  90. };
  91. static const struct udevice_id generic_syscon_ids[] = {
  92. { .compatible = "syscon" },
  93. { }
  94. };
  95. U_BOOT_DRIVER(generic_syscon) = {
  96. .name = "syscon",
  97. .id = UCLASS_SYSCON,
  98. #if !CONFIG_IS_ENABLED(OF_PLATDATA)
  99. .bind = dm_scan_fdt_dev,
  100. #endif
  101. .of_match = generic_syscon_ids,
  102. };
  103. /*
  104. * Linux-compatible syscon-to-regmap
  105. * The syscon node can be bound to another driver, but still works
  106. * as a syscon provider.
  107. */
  108. static LIST_HEAD(syscon_list);
  109. struct syscon {
  110. ofnode node;
  111. struct regmap *regmap;
  112. struct list_head list;
  113. };
  114. static struct syscon *of_syscon_register(ofnode node)
  115. {
  116. struct syscon *syscon;
  117. int ret;
  118. if (!ofnode_device_is_compatible(node, "syscon"))
  119. return ERR_PTR(-EINVAL);
  120. syscon = malloc(sizeof(*syscon));
  121. if (!syscon)
  122. return ERR_PTR(-ENOMEM);
  123. ret = regmap_init_mem(node, &syscon->regmap);
  124. if (ret) {
  125. free(syscon);
  126. return ERR_PTR(ret);
  127. }
  128. list_add_tail(&syscon->list, &syscon_list);
  129. return syscon;
  130. }
  131. struct regmap *syscon_node_to_regmap(ofnode node)
  132. {
  133. struct syscon *entry, *syscon = NULL;
  134. list_for_each_entry(entry, &syscon_list, list)
  135. if (ofnode_equal(entry->node, node)) {
  136. syscon = entry;
  137. break;
  138. }
  139. if (!syscon)
  140. syscon = of_syscon_register(node);
  141. if (IS_ERR(syscon))
  142. return ERR_CAST(syscon);
  143. return syscon->regmap;
  144. }