rcar_du_of.c 8.0 KB

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