syscon-uclass.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. #if CONFIG_IS_ENABLED(OF_PLATDATA)
  46. /*
  47. * With OF_PLATDATA we really have no way of knowing the format of
  48. * the device-specific platform data. So we assume that it starts with
  49. * a 'reg' member that holds a single address and size. Drivers
  50. * using OF_PLATDATA will need to ensure that this is true. In case of
  51. * odd reg structures other then the syscon_base_plat structure
  52. * below the regmap must be defined in the individual syscon driver.
  53. */
  54. struct syscon_base_plat {
  55. phys_addr_t reg[2];
  56. };
  57. struct syscon_base_plat *plat = dev_get_plat(dev);
  58. /*
  59. * Return if the regmap is already defined in the individual
  60. * syscon driver.
  61. */
  62. if (priv->regmap)
  63. return 0;
  64. return regmap_init_mem_plat(dev, plat->reg, sizeof(plat->reg[0]),
  65. ARRAY_SIZE(plat->reg) / 2, &priv->regmap);
  66. #else
  67. return regmap_init_mem(dev_ofnode(dev), &priv->regmap);
  68. #endif
  69. }
  70. static int syscon_probe_by_ofnode(ofnode node, struct udevice **devp)
  71. {
  72. struct udevice *dev, *parent;
  73. int ret;
  74. /* found node with "syscon" compatible, not bounded to SYSCON UCLASS */
  75. if (!ofnode_device_is_compatible(node, "syscon")) {
  76. log_debug("invalid compatible for syscon device\n");
  77. return -EINVAL;
  78. }
  79. /* bound to driver with same ofnode or to root if not found */
  80. if (device_find_global_by_ofnode(node, &parent))
  81. parent = dm_root();
  82. /* force bound to syscon class */
  83. ret = device_bind_driver_to_node(parent, "syscon",
  84. ofnode_get_name(node),
  85. node, &dev);
  86. if (ret) {
  87. dev_dbg(dev, "unable to bound syscon device\n");
  88. return ret;
  89. }
  90. ret = device_probe(dev);
  91. if (ret) {
  92. dev_dbg(dev, "unable to probe syscon device\n");
  93. return ret;
  94. }
  95. *devp = dev;
  96. return 0;
  97. }
  98. struct regmap *syscon_regmap_lookup_by_phandle(struct udevice *dev,
  99. const char *name)
  100. {
  101. struct udevice *syscon;
  102. struct regmap *r;
  103. u32 phandle;
  104. ofnode node;
  105. int err;
  106. err = uclass_get_device_by_phandle(UCLASS_SYSCON, dev,
  107. name, &syscon);
  108. if (err) {
  109. /* found node with "syscon" compatible, not bounded to SYSCON */
  110. err = ofnode_read_u32(dev_ofnode(dev), name, &phandle);
  111. if (err)
  112. return ERR_PTR(err);
  113. node = ofnode_get_by_phandle(phandle);
  114. if (!ofnode_valid(node)) {
  115. dev_dbg(dev, "unable to find syscon device\n");
  116. return ERR_PTR(-EINVAL);
  117. }
  118. err = syscon_probe_by_ofnode(node, &syscon);
  119. if (err)
  120. return ERR_PTR(-ENODEV);
  121. }
  122. r = syscon_get_regmap(syscon);
  123. if (!r) {
  124. dev_dbg(dev, "unable to find regmap\n");
  125. return ERR_PTR(-ENODEV);
  126. }
  127. return r;
  128. }
  129. int syscon_get_by_driver_data(ulong driver_data, struct udevice **devp)
  130. {
  131. int ret;
  132. *devp = NULL;
  133. ret = uclass_first_device_drvdata(UCLASS_SYSCON, driver_data, devp);
  134. if (ret)
  135. return ret;
  136. return 0;
  137. }
  138. struct regmap *syscon_get_regmap_by_driver_data(ulong driver_data)
  139. {
  140. struct syscon_uc_info *priv;
  141. struct udevice *dev;
  142. int ret;
  143. ret = syscon_get_by_driver_data(driver_data, &dev);
  144. if (ret)
  145. return ERR_PTR(ret);
  146. priv = dev_get_uclass_priv(dev);
  147. return priv->regmap;
  148. }
  149. void *syscon_get_first_range(ulong driver_data)
  150. {
  151. struct regmap *map;
  152. map = syscon_get_regmap_by_driver_data(driver_data);
  153. if (IS_ERR(map))
  154. return map;
  155. return regmap_get_range(map, 0);
  156. }
  157. UCLASS_DRIVER(syscon) = {
  158. .id = UCLASS_SYSCON,
  159. .name = "syscon",
  160. .per_device_auto = sizeof(struct syscon_uc_info),
  161. .pre_probe = syscon_pre_probe,
  162. };
  163. static const struct udevice_id generic_syscon_ids[] = {
  164. { .compatible = "syscon" },
  165. { }
  166. };
  167. U_BOOT_DRIVER(generic_syscon) = {
  168. .name = "syscon",
  169. .id = UCLASS_SYSCON,
  170. #if CONFIG_IS_ENABLED(OF_REAL)
  171. .bind = dm_scan_fdt_dev,
  172. #endif
  173. .of_match = generic_syscon_ids,
  174. };
  175. /*
  176. * Linux-compatible syscon-to-regmap
  177. * The syscon node can be bound to another driver, but still works
  178. * as a syscon provider.
  179. */
  180. struct regmap *syscon_node_to_regmap(ofnode node)
  181. {
  182. struct udevice *dev;
  183. struct regmap *r;
  184. if (uclass_get_device_by_ofnode(UCLASS_SYSCON, node, &dev))
  185. if (syscon_probe_by_ofnode(node, &dev))
  186. return ERR_PTR(-ENODEV);
  187. r = syscon_get_regmap(dev);
  188. if (!r) {
  189. dev_dbg(dev, "unable to find regmap\n");
  190. return ERR_PTR(-ENODEV);
  191. }
  192. return r;
  193. }