syscon-uclass.c 4.8 KB

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