syscon-uclass.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. struct regmap *syscon_regmap_lookup_by_phandle(struct udevice *dev,
  50. const char *name)
  51. {
  52. struct udevice *syscon;
  53. struct regmap *r;
  54. int err;
  55. err = uclass_get_device_by_phandle(UCLASS_SYSCON, dev,
  56. name, &syscon);
  57. if (err) {
  58. dev_dbg(dev, "unable to find syscon device\n");
  59. return ERR_PTR(err);
  60. }
  61. r = syscon_get_regmap(syscon);
  62. if (!r) {
  63. dev_dbg(dev, "unable to find regmap\n");
  64. return ERR_PTR(-ENODEV);
  65. }
  66. return r;
  67. }
  68. int syscon_get_by_driver_data(ulong driver_data, struct udevice **devp)
  69. {
  70. struct udevice *dev;
  71. struct uclass *uc;
  72. int ret;
  73. *devp = NULL;
  74. ret = uclass_get(UCLASS_SYSCON, &uc);
  75. if (ret)
  76. return ret;
  77. uclass_foreach_dev(dev, uc) {
  78. if (dev->driver_data == driver_data) {
  79. *devp = dev;
  80. return device_probe(dev);
  81. }
  82. }
  83. return -ENODEV;
  84. }
  85. struct regmap *syscon_get_regmap_by_driver_data(ulong driver_data)
  86. {
  87. struct syscon_uc_info *priv;
  88. struct udevice *dev;
  89. int ret;
  90. ret = syscon_get_by_driver_data(driver_data, &dev);
  91. if (ret)
  92. return ERR_PTR(ret);
  93. priv = dev_get_uclass_priv(dev);
  94. return priv->regmap;
  95. }
  96. void *syscon_get_first_range(ulong driver_data)
  97. {
  98. struct regmap *map;
  99. map = syscon_get_regmap_by_driver_data(driver_data);
  100. if (IS_ERR(map))
  101. return map;
  102. return regmap_get_range(map, 0);
  103. }
  104. UCLASS_DRIVER(syscon) = {
  105. .id = UCLASS_SYSCON,
  106. .name = "syscon",
  107. .per_device_auto_alloc_size = sizeof(struct syscon_uc_info),
  108. .pre_probe = syscon_pre_probe,
  109. };
  110. static const struct udevice_id generic_syscon_ids[] = {
  111. { .compatible = "syscon" },
  112. { }
  113. };
  114. U_BOOT_DRIVER(generic_syscon) = {
  115. .name = "syscon",
  116. .id = UCLASS_SYSCON,
  117. #if !CONFIG_IS_ENABLED(OF_PLATDATA)
  118. .bind = dm_scan_fdt_dev,
  119. #endif
  120. .of_match = generic_syscon_ids,
  121. };
  122. /*
  123. * Linux-compatible syscon-to-regmap
  124. * The syscon node can be bound to another driver, but still works
  125. * as a syscon provider.
  126. */
  127. static LIST_HEAD(syscon_list);
  128. struct syscon {
  129. ofnode node;
  130. struct regmap *regmap;
  131. struct list_head list;
  132. };
  133. static struct syscon *of_syscon_register(ofnode node)
  134. {
  135. struct syscon *syscon;
  136. int ret;
  137. if (!ofnode_device_is_compatible(node, "syscon"))
  138. return ERR_PTR(-EINVAL);
  139. syscon = malloc(sizeof(*syscon));
  140. if (!syscon)
  141. return ERR_PTR(-ENOMEM);
  142. ret = regmap_init_mem(node, &syscon->regmap);
  143. if (ret) {
  144. free(syscon);
  145. return ERR_PTR(ret);
  146. }
  147. list_add_tail(&syscon->list, &syscon_list);
  148. return syscon;
  149. }
  150. struct regmap *syscon_node_to_regmap(ofnode node)
  151. {
  152. struct syscon *entry, *syscon = NULL;
  153. list_for_each_entry(entry, &syscon_list, list)
  154. if (ofnode_equal(entry->node, node)) {
  155. syscon = entry;
  156. break;
  157. }
  158. if (!syscon)
  159. syscon = of_syscon_register(node);
  160. if (IS_ERR(syscon))
  161. return ERR_CAST(syscon);
  162. return syscon->regmap;
  163. }