rcar_du_of.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * rcar_du_of.c - Legacy DT bindings compatibility
  4. *
  5. * Copyright (C) 2018 Laurent Pinchart <laurent.pinchart@ideasonboard.com>
  6. *
  7. * Based on work from Jyri Sarha <jsarha@ti.com>
  8. * Copyright (C) 2015 Texas Instruments
  9. */
  10. #include <linux/init.h>
  11. #include <linux/kernel.h>
  12. #include <linux/of.h>
  13. #include <linux/of_address.h>
  14. #include <linux/of_fdt.h>
  15. #include <linux/of_graph.h>
  16. #include <linux/slab.h>
  17. #include "rcar_du_crtc.h"
  18. #include "rcar_du_drv.h"
  19. /* -----------------------------------------------------------------------------
  20. * Generic Overlay Handling
  21. */
  22. struct rcar_du_of_overlay {
  23. const char *compatible;
  24. void *begin;
  25. void *end;
  26. };
  27. #define RCAR_DU_OF_DTB(type, soc) \
  28. extern char __dtb_rcar_du_of_##type##_##soc##_begin[]; \
  29. extern char __dtb_rcar_du_of_##type##_##soc##_end[]
  30. #define RCAR_DU_OF_OVERLAY(type, soc) \
  31. { \
  32. .compatible = "renesas,du-" #soc, \
  33. .begin = __dtb_rcar_du_of_##type##_##soc##_begin, \
  34. .end = __dtb_rcar_du_of_##type##_##soc##_end, \
  35. }
  36. static int __init rcar_du_of_apply_overlay(const struct rcar_du_of_overlay *dtbs,
  37. const char *compatible)
  38. {
  39. const struct rcar_du_of_overlay *dtb = NULL;
  40. unsigned int i;
  41. int ovcs_id;
  42. for (i = 0; dtbs[i].compatible; ++i) {
  43. if (!strcmp(dtbs[i].compatible, compatible)) {
  44. dtb = &dtbs[i];
  45. break;
  46. }
  47. }
  48. if (!dtb)
  49. return -ENODEV;
  50. ovcs_id = 0;
  51. return of_overlay_fdt_apply(dtb->begin, dtb->end - dtb->begin,
  52. &ovcs_id);
  53. }
  54. static int __init rcar_du_of_add_property(struct of_changeset *ocs,
  55. struct device_node *np,
  56. const char *name, const void *value,
  57. int length)
  58. {
  59. struct property *prop;
  60. int ret = -ENOMEM;
  61. prop = kzalloc(sizeof(*prop), GFP_KERNEL);
  62. if (!prop)
  63. return -ENOMEM;
  64. prop->name = kstrdup(name, GFP_KERNEL);
  65. if (!prop->name)
  66. goto out_err;
  67. prop->value = kmemdup(value, length, GFP_KERNEL);
  68. if (!prop->value)
  69. goto out_err;
  70. of_property_set_flag(prop, OF_DYNAMIC);
  71. prop->length = length;
  72. ret = of_changeset_add_property(ocs, np, prop);
  73. if (!ret)
  74. return 0;
  75. out_err:
  76. kfree(prop->value);
  77. kfree(prop->name);
  78. kfree(prop);
  79. return ret;
  80. }
  81. /* -----------------------------------------------------------------------------
  82. * LVDS Overlays
  83. */
  84. RCAR_DU_OF_DTB(lvds, r8a7790);
  85. RCAR_DU_OF_DTB(lvds, r8a7791);
  86. RCAR_DU_OF_DTB(lvds, r8a7793);
  87. RCAR_DU_OF_DTB(lvds, r8a7795);
  88. RCAR_DU_OF_DTB(lvds, r8a7796);
  89. static const struct rcar_du_of_overlay rcar_du_lvds_overlays[] __initconst = {
  90. RCAR_DU_OF_OVERLAY(lvds, r8a7790),
  91. RCAR_DU_OF_OVERLAY(lvds, r8a7791),
  92. RCAR_DU_OF_OVERLAY(lvds, r8a7793),
  93. RCAR_DU_OF_OVERLAY(lvds, r8a7795),
  94. RCAR_DU_OF_OVERLAY(lvds, r8a7796),
  95. { /* Sentinel */ },
  96. };
  97. static struct of_changeset rcar_du_lvds_changeset;
  98. static void __init rcar_du_of_lvds_patch_one(struct device_node *lvds,
  99. const struct of_phandle_args *clk,
  100. struct device_node *local,
  101. struct device_node *remote)
  102. {
  103. unsigned int psize;
  104. unsigned int i;
  105. __be32 value[4];
  106. int ret;
  107. /*
  108. * Set the LVDS clocks property. This can't be performed by the overlay
  109. * as the structure of the clock specifier has changed over time, and we
  110. * don't know at compile time which binding version the system we will
  111. * run on uses.
  112. */
  113. if (clk->args_count >= ARRAY_SIZE(value) - 1)
  114. return;
  115. of_changeset_init(&rcar_du_lvds_changeset);
  116. value[0] = cpu_to_be32(clk->np->phandle);
  117. for (i = 0; i < clk->args_count; ++i)
  118. value[i + 1] = cpu_to_be32(clk->args[i]);
  119. psize = (clk->args_count + 1) * 4;
  120. ret = rcar_du_of_add_property(&rcar_du_lvds_changeset, lvds,
  121. "clocks", value, psize);
  122. if (ret < 0)
  123. goto done;
  124. /*
  125. * Insert the node in the OF graph: patch the LVDS ports remote-endpoint
  126. * properties to point to the endpoints of the sibling nodes in the
  127. * graph. This can't be performed by the overlay: on the input side the
  128. * overlay would contain a phandle for the DU LVDS output port that
  129. * would clash with the system DT, and on the output side the connection
  130. * is board-specific.
  131. */
  132. value[0] = cpu_to_be32(local->phandle);
  133. value[1] = cpu_to_be32(remote->phandle);
  134. for (i = 0; i < 2; ++i) {
  135. struct device_node *endpoint;
  136. endpoint = of_graph_get_endpoint_by_regs(lvds, i, 0);
  137. if (!endpoint) {
  138. ret = -EINVAL;
  139. goto done;
  140. }
  141. ret = rcar_du_of_add_property(&rcar_du_lvds_changeset,
  142. endpoint, "remote-endpoint",
  143. &value[i], sizeof(value[i]));
  144. of_node_put(endpoint);
  145. if (ret < 0)
  146. goto done;
  147. }
  148. ret = of_changeset_apply(&rcar_du_lvds_changeset);
  149. done:
  150. if (ret < 0)
  151. of_changeset_destroy(&rcar_du_lvds_changeset);
  152. }
  153. struct lvds_of_data {
  154. struct resource res;
  155. struct of_phandle_args clkspec;
  156. struct device_node *local;
  157. struct device_node *remote;
  158. };
  159. static void __init rcar_du_of_lvds_patch(const struct of_device_id *of_ids)
  160. {
  161. const struct rcar_du_device_info *info;
  162. const struct of_device_id *match;
  163. struct lvds_of_data lvds_data[2] = { };
  164. struct device_node *lvds_node;
  165. struct device_node *soc_node;
  166. struct device_node *du_node;
  167. char compatible[22];
  168. const char *soc_name;
  169. unsigned int i;
  170. int ret;
  171. /* Get the DU node and exit if not present or disabled. */
  172. du_node = of_find_matching_node_and_match(NULL, of_ids, &match);
  173. if (!du_node || !of_device_is_available(du_node)) {
  174. of_node_put(du_node);
  175. return;
  176. }
  177. info = match->data;
  178. soc_node = of_get_parent(du_node);
  179. if (WARN_ON(info->num_lvds > ARRAY_SIZE(lvds_data)))
  180. goto done;
  181. /*
  182. * Skip if the LVDS nodes already exists.
  183. *
  184. * The nodes are searched based on the compatible string, which we
  185. * construct from the SoC name found in the DU compatible string. As a
  186. * match has been found we know the compatible string matches the
  187. * expected format and can thus skip some of the string manipulation
  188. * normal safety checks.
  189. */
  190. soc_name = strchr(match->compatible, '-') + 1;
  191. sprintf(compatible, "renesas,%s-lvds", soc_name);
  192. lvds_node = of_find_compatible_node(NULL, NULL, compatible);
  193. if (lvds_node) {
  194. of_node_put(lvds_node);
  195. return;
  196. }
  197. /*
  198. * Parse the DU node and store the register specifier, the clock
  199. * specifier and the local and remote endpoint of the LVDS link for
  200. * later use.
  201. */
  202. for (i = 0; i < info->num_lvds; ++i) {
  203. struct lvds_of_data *lvds = &lvds_data[i];
  204. unsigned int port;
  205. char name[7];
  206. int index;
  207. sprintf(name, "lvds.%u", i);
  208. index = of_property_match_string(du_node, "clock-names", name);
  209. if (index < 0)
  210. continue;
  211. ret = of_parse_phandle_with_args(du_node, "clocks",
  212. "#clock-cells", index,
  213. &lvds->clkspec);
  214. if (ret < 0)
  215. continue;
  216. port = info->routes[RCAR_DU_OUTPUT_LVDS0 + i].port;
  217. lvds->local = of_graph_get_endpoint_by_regs(du_node, port, 0);
  218. if (!lvds->local)
  219. continue;
  220. lvds->remote = of_graph_get_remote_endpoint(lvds->local);
  221. if (!lvds->remote)
  222. continue;
  223. index = of_property_match_string(du_node, "reg-names", name);
  224. if (index < 0)
  225. continue;
  226. of_address_to_resource(du_node, index, &lvds->res);
  227. }
  228. /* Parse and apply the overlay. This will resolve phandles. */
  229. ret = rcar_du_of_apply_overlay(rcar_du_lvds_overlays,
  230. match->compatible);
  231. if (ret < 0)
  232. goto done;
  233. /* Patch the newly created LVDS encoder nodes. */
  234. for_each_child_of_node(soc_node, lvds_node) {
  235. struct resource res;
  236. if (!of_device_is_compatible(lvds_node, compatible))
  237. continue;
  238. /* Locate the lvds_data entry based on the resource start. */
  239. ret = of_address_to_resource(lvds_node, 0, &res);
  240. if (ret < 0)
  241. continue;
  242. for (i = 0; i < ARRAY_SIZE(lvds_data); ++i) {
  243. if (lvds_data[i].res.start == res.start)
  244. break;
  245. }
  246. if (i == ARRAY_SIZE(lvds_data))
  247. continue;
  248. /* Patch the LVDS encoder. */
  249. rcar_du_of_lvds_patch_one(lvds_node, &lvds_data[i].clkspec,
  250. lvds_data[i].local,
  251. lvds_data[i].remote);
  252. }
  253. done:
  254. for (i = 0; i < info->num_lvds; ++i) {
  255. of_node_put(lvds_data[i].clkspec.np);
  256. of_node_put(lvds_data[i].local);
  257. of_node_put(lvds_data[i].remote);
  258. }
  259. of_node_put(soc_node);
  260. of_node_put(du_node);
  261. }
  262. void __init rcar_du_of_init(const struct of_device_id *of_ids)
  263. {
  264. rcar_du_of_lvds_patch(of_ids);
  265. }