syscon-uclass.c 4.8 KB

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