syscon-uclass.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 <log.h>
  8. #include <syscon.h>
  9. #include <dm.h>
  10. #include <errno.h>
  11. #include <regmap.h>
  12. #include <dm/device-internal.h>
  13. #include <dm/device_compat.h>
  14. #include <dm/lists.h>
  15. #include <dm/root.h>
  16. #include <linux/err.h>
  17. /*
  18. * Caution:
  19. * This API requires the given device has alerady been bound to syscon driver.
  20. * For example,
  21. * compatible = "syscon", "simple-mfd";
  22. * works, but
  23. * compatible = "simple-mfd", "syscon";
  24. * does not. The behavior is different from Linux.
  25. */
  26. struct regmap *syscon_get_regmap(struct udevice *dev)
  27. {
  28. struct syscon_uc_info *priv;
  29. if (device_get_uclass_id(dev) != UCLASS_SYSCON)
  30. return ERR_PTR(-ENOEXEC);
  31. priv = dev_get_uclass_priv(dev);
  32. return priv->regmap;
  33. }
  34. static int syscon_pre_probe(struct udevice *dev)
  35. {
  36. struct syscon_uc_info *priv = dev_get_uclass_priv(dev);
  37. /* Special case for PCI devices, which don't have a regmap */
  38. if (device_get_uclass_id(dev->parent) == UCLASS_PCI)
  39. return 0;
  40. /*
  41. * With OF_PLATDATA we really have no way of knowing the format of
  42. * the device-specific platform data. So we assume that it starts with
  43. * a 'reg' member, and this holds a single address and size. Drivers
  44. * using OF_PLATDATA will need to ensure that this is true.
  45. */
  46. #if CONFIG_IS_ENABLED(OF_PLATDATA)
  47. struct syscon_base_platdata *plat = dev_get_platdata(dev);
  48. return regmap_init_mem_platdata(dev, plat->reg, ARRAY_SIZE(plat->reg),
  49. &priv->regmap);
  50. #else
  51. return regmap_init_mem(dev_ofnode(dev), &priv->regmap);
  52. #endif
  53. }
  54. static int syscon_probe_by_ofnode(ofnode node, struct udevice **devp)
  55. {
  56. struct udevice *dev, *parent;
  57. int ret;
  58. /* found node with "syscon" compatible, not bounded to SYSCON UCLASS */
  59. if (!ofnode_device_is_compatible(node, "syscon")) {
  60. dev_dbg(dev, "invalid compatible for syscon device\n");
  61. return -EINVAL;
  62. }
  63. /* bound to driver with same ofnode or to root if not found */
  64. if (device_find_global_by_ofnode(node, &parent))
  65. parent = dm_root();
  66. /* force bound to syscon class */
  67. ret = device_bind_driver_to_node(parent, "syscon",
  68. ofnode_get_name(node),
  69. node, &dev);
  70. if (ret) {
  71. dev_dbg(dev, "unable to bound syscon device\n");
  72. return ret;
  73. }
  74. ret = device_probe(dev);
  75. if (ret) {
  76. dev_dbg(dev, "unable to probe syscon device\n");
  77. return ret;
  78. }
  79. *devp = dev;
  80. return 0;
  81. }
  82. struct regmap *syscon_regmap_lookup_by_phandle(struct udevice *dev,
  83. const char *name)
  84. {
  85. struct udevice *syscon;
  86. struct regmap *r;
  87. u32 phandle;
  88. ofnode node;
  89. int err;
  90. err = uclass_get_device_by_phandle(UCLASS_SYSCON, dev,
  91. name, &syscon);
  92. if (err) {
  93. /* found node with "syscon" compatible, not bounded to SYSCON */
  94. err = ofnode_read_u32(dev_ofnode(dev), name, &phandle);
  95. if (err)
  96. return ERR_PTR(err);
  97. node = ofnode_get_by_phandle(phandle);
  98. if (!ofnode_valid(node)) {
  99. dev_dbg(dev, "unable to find syscon device\n");
  100. return ERR_PTR(-EINVAL);
  101. }
  102. err = syscon_probe_by_ofnode(node, &syscon);
  103. if (err)
  104. return ERR_PTR(-ENODEV);
  105. }
  106. r = syscon_get_regmap(syscon);
  107. if (!r) {
  108. dev_dbg(dev, "unable to find regmap\n");
  109. return ERR_PTR(-ENODEV);
  110. }
  111. return r;
  112. }
  113. int syscon_get_by_driver_data(ulong driver_data, struct udevice **devp)
  114. {
  115. int ret;
  116. *devp = NULL;
  117. ret = uclass_first_device_drvdata(UCLASS_SYSCON, driver_data, devp);
  118. if (ret)
  119. return log_msg_ret("find", ret);
  120. return 0;
  121. }
  122. struct regmap *syscon_get_regmap_by_driver_data(ulong driver_data)
  123. {
  124. struct syscon_uc_info *priv;
  125. struct udevice *dev;
  126. int ret;
  127. ret = syscon_get_by_driver_data(driver_data, &dev);
  128. if (ret)
  129. return ERR_PTR(ret);
  130. priv = dev_get_uclass_priv(dev);
  131. return priv->regmap;
  132. }
  133. void *syscon_get_first_range(ulong driver_data)
  134. {
  135. struct regmap *map;
  136. map = syscon_get_regmap_by_driver_data(driver_data);
  137. if (IS_ERR(map))
  138. return map;
  139. return regmap_get_range(map, 0);
  140. }
  141. UCLASS_DRIVER(syscon) = {
  142. .id = UCLASS_SYSCON,
  143. .name = "syscon",
  144. .per_device_auto_alloc_size = sizeof(struct syscon_uc_info),
  145. .pre_probe = syscon_pre_probe,
  146. };
  147. static const struct udevice_id generic_syscon_ids[] = {
  148. { .compatible = "syscon" },
  149. { }
  150. };
  151. U_BOOT_DRIVER(generic_syscon) = {
  152. .name = "syscon",
  153. .id = UCLASS_SYSCON,
  154. #if !CONFIG_IS_ENABLED(OF_PLATDATA)
  155. .bind = dm_scan_fdt_dev,
  156. #endif
  157. .of_match = generic_syscon_ids,
  158. };
  159. /*
  160. * Linux-compatible syscon-to-regmap
  161. * The syscon node can be bound to another driver, but still works
  162. * as a syscon provider.
  163. */
  164. struct regmap *syscon_node_to_regmap(ofnode node)
  165. {
  166. struct udevice *dev;
  167. struct regmap *r;
  168. if (uclass_get_device_by_ofnode(UCLASS_SYSCON, node, &dev))
  169. if (syscon_probe_by_ofnode(node, &dev))
  170. return ERR_PTR(-ENODEV);
  171. r = syscon_get_regmap(dev);
  172. if (!r) {
  173. dev_dbg(dev, "unable to find regmap\n");
  174. return ERR_PTR(-ENODEV);
  175. }
  176. return r;
  177. }