omapdss-boot-init.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * Copyright (C) 2014 Texas Instruments
  3. * Author: Tomi Valkeinen <tomi.valkeinen@ti.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published by
  7. * the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /*
  18. * As omapdss panel drivers are omapdss specific, but we want to define the
  19. * DT-data in generic manner, we convert the compatible strings of the panel and
  20. * encoder nodes from "panel-foo" to "omapdss,panel-foo". This way we can have
  21. * both correct DT data and omapdss specific drivers.
  22. *
  23. * When we get generic panel drivers to the kernel, this file will be removed.
  24. */
  25. #include <linux/kernel.h>
  26. #include <linux/of.h>
  27. #include <linux/of_graph.h>
  28. #include <linux/slab.h>
  29. #include <linux/list.h>
  30. static struct list_head dss_conv_list __initdata;
  31. static const char prefix[] __initconst = "omapdss,";
  32. struct dss_conv_node {
  33. struct list_head list;
  34. struct device_node *node;
  35. bool root;
  36. };
  37. static int __init omapdss_count_strings(const struct property *prop)
  38. {
  39. const char *p = prop->value;
  40. int l = 0, total = 0;
  41. int i;
  42. for (i = 0; total < prop->length; total += l, p += l, i++)
  43. l = strlen(p) + 1;
  44. return i;
  45. }
  46. static void __init omapdss_update_prop(struct device_node *node, char *compat,
  47. int len)
  48. {
  49. struct property *prop;
  50. prop = kzalloc(sizeof(*prop), GFP_KERNEL);
  51. if (!prop)
  52. return;
  53. prop->name = "compatible";
  54. prop->value = compat;
  55. prop->length = len;
  56. of_update_property(node, prop);
  57. }
  58. static void __init omapdss_prefix_strcpy(char *dst, int dst_len,
  59. const char *src, int src_len)
  60. {
  61. size_t total = 0;
  62. while (total < src_len) {
  63. size_t l = strlen(src) + 1;
  64. strcpy(dst, prefix);
  65. dst += strlen(prefix);
  66. strcpy(dst, src);
  67. dst += l;
  68. src += l;
  69. total += l;
  70. }
  71. }
  72. /* prepend compatible property strings with "omapdss," */
  73. static void __init omapdss_omapify_node(struct device_node *node)
  74. {
  75. struct property *prop;
  76. char *new_compat;
  77. int num_strs;
  78. int new_len;
  79. prop = of_find_property(node, "compatible", NULL);
  80. if (!prop || !prop->value)
  81. return;
  82. if (strnlen(prop->value, prop->length) >= prop->length)
  83. return;
  84. /* is it already prefixed? */
  85. if (strncmp(prefix, prop->value, strlen(prefix)) == 0)
  86. return;
  87. num_strs = omapdss_count_strings(prop);
  88. new_len = prop->length + strlen(prefix) * num_strs;
  89. new_compat = kmalloc(new_len, GFP_KERNEL);
  90. omapdss_prefix_strcpy(new_compat, new_len, prop->value, prop->length);
  91. omapdss_update_prop(node, new_compat, new_len);
  92. }
  93. static void __init omapdss_add_to_list(struct device_node *node, bool root)
  94. {
  95. struct dss_conv_node *n = kmalloc(sizeof(struct dss_conv_node),
  96. GFP_KERNEL);
  97. if (n) {
  98. n->node = node;
  99. n->root = root;
  100. list_add(&n->list, &dss_conv_list);
  101. }
  102. }
  103. static bool __init omapdss_list_contains(const struct device_node *node)
  104. {
  105. struct dss_conv_node *n;
  106. list_for_each_entry(n, &dss_conv_list, list) {
  107. if (n->node == node)
  108. return true;
  109. }
  110. return false;
  111. }
  112. static void __init omapdss_walk_device(struct device_node *node, bool root)
  113. {
  114. struct device_node *n;
  115. omapdss_add_to_list(node, root);
  116. /*
  117. * of_graph_get_remote_port_parent() prints an error if there is no
  118. * port/ports node. To avoid that, check first that there's the node.
  119. */
  120. n = of_get_child_by_name(node, "ports");
  121. if (!n)
  122. n = of_get_child_by_name(node, "port");
  123. if (!n)
  124. return;
  125. of_node_put(n);
  126. n = NULL;
  127. while ((n = of_graph_get_next_endpoint(node, n)) != NULL) {
  128. struct device_node *pn;
  129. pn = of_graph_get_remote_port_parent(n);
  130. if (!pn)
  131. continue;
  132. if (!of_device_is_available(pn) || omapdss_list_contains(pn)) {
  133. of_node_put(pn);
  134. continue;
  135. }
  136. omapdss_walk_device(pn, false);
  137. }
  138. }
  139. static const struct of_device_id omapdss_of_match[] __initconst = {
  140. { .compatible = "ti,omap2-dss", },
  141. { .compatible = "ti,omap3-dss", },
  142. { .compatible = "ti,omap4-dss", },
  143. { .compatible = "ti,omap5-dss", },
  144. { .compatible = "ti,dra7-dss", },
  145. {},
  146. };
  147. static int __init omapdss_boot_init(void)
  148. {
  149. struct device_node *dss, *child;
  150. INIT_LIST_HEAD(&dss_conv_list);
  151. dss = of_find_matching_node(NULL, omapdss_of_match);
  152. if (dss == NULL || !of_device_is_available(dss))
  153. return 0;
  154. omapdss_walk_device(dss, true);
  155. for_each_available_child_of_node(dss, child) {
  156. if (!of_find_property(child, "compatible", NULL)) {
  157. of_node_put(child);
  158. continue;
  159. }
  160. omapdss_walk_device(child, true);
  161. }
  162. while (!list_empty(&dss_conv_list)) {
  163. struct dss_conv_node *n;
  164. n = list_first_entry(&dss_conv_list, struct dss_conv_node,
  165. list);
  166. if (!n->root)
  167. omapdss_omapify_node(n->node);
  168. list_del(&n->list);
  169. of_node_put(n->node);
  170. kfree(n);
  171. }
  172. return 0;
  173. }
  174. subsys_initcall(omapdss_boot_init);