tilcdc_slave_compat.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * Copyright (C) 2015 Texas Instruments
  3. * Author: Jyri Sarha <jsarha@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. */
  10. /*
  11. * To support the old "ti,tilcdc,slave" binding the binding has to be
  12. * transformed to the new external encoder binding.
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/of.h>
  16. #include <linux/of_graph.h>
  17. #include <linux/of_fdt.h>
  18. #include <linux/slab.h>
  19. #include <linux/list.h>
  20. #include "tilcdc_slave_compat.h"
  21. struct kfree_table {
  22. int total;
  23. int num;
  24. void **table;
  25. };
  26. static int __init kfree_table_init(struct kfree_table *kft)
  27. {
  28. kft->total = 32;
  29. kft->num = 0;
  30. kft->table = kmalloc(kft->total * sizeof(*kft->table),
  31. GFP_KERNEL);
  32. if (!kft->table)
  33. return -ENOMEM;
  34. return 0;
  35. }
  36. static int __init kfree_table_add(struct kfree_table *kft, void *p)
  37. {
  38. if (kft->num == kft->total) {
  39. void **old = kft->table;
  40. kft->total *= 2;
  41. kft->table = krealloc(old, kft->total * sizeof(*kft->table),
  42. GFP_KERNEL);
  43. if (!kft->table) {
  44. kft->table = old;
  45. kfree(p);
  46. return -ENOMEM;
  47. }
  48. }
  49. kft->table[kft->num++] = p;
  50. return 0;
  51. }
  52. static void __init kfree_table_free(struct kfree_table *kft)
  53. {
  54. int i;
  55. for (i = 0; i < kft->num; i++)
  56. kfree(kft->table[i]);
  57. kfree(kft->table);
  58. }
  59. static
  60. struct property * __init tilcdc_prop_dup(const struct property *prop,
  61. struct kfree_table *kft)
  62. {
  63. struct property *nprop;
  64. nprop = kzalloc(sizeof(*nprop), GFP_KERNEL);
  65. if (!nprop || kfree_table_add(kft, nprop))
  66. return NULL;
  67. nprop->name = kstrdup(prop->name, GFP_KERNEL);
  68. if (!nprop->name || kfree_table_add(kft, nprop->name))
  69. return NULL;
  70. nprop->value = kmemdup(prop->value, prop->length, GFP_KERNEL);
  71. if (!nprop->value || kfree_table_add(kft, nprop->value))
  72. return NULL;
  73. nprop->length = prop->length;
  74. return nprop;
  75. }
  76. static void __init tilcdc_copy_props(struct device_node *from,
  77. struct device_node *to,
  78. const char * const props[],
  79. struct kfree_table *kft)
  80. {
  81. struct property *prop;
  82. int i;
  83. for (i = 0; props[i]; i++) {
  84. prop = of_find_property(from, props[i], NULL);
  85. if (!prop)
  86. continue;
  87. prop = tilcdc_prop_dup(prop, kft);
  88. if (!prop)
  89. continue;
  90. prop->next = to->properties;
  91. to->properties = prop;
  92. }
  93. }
  94. static int __init tilcdc_prop_str_update(struct property *prop,
  95. const char *str,
  96. struct kfree_table *kft)
  97. {
  98. prop->value = kstrdup(str, GFP_KERNEL);
  99. if (kfree_table_add(kft, prop->value) || !prop->value)
  100. return -ENOMEM;
  101. prop->length = strlen(str)+1;
  102. return 0;
  103. }
  104. static void __init tilcdc_node_disable(struct device_node *node)
  105. {
  106. struct property *prop;
  107. prop = kzalloc(sizeof(*prop), GFP_KERNEL);
  108. if (!prop)
  109. return;
  110. prop->name = "status";
  111. prop->value = "disabled";
  112. prop->length = strlen((char *)prop->value)+1;
  113. of_update_property(node, prop);
  114. }
  115. struct device_node * __init tilcdc_get_overlay(struct kfree_table *kft)
  116. {
  117. const int size = __dtb_tilcdc_slave_compat_end -
  118. __dtb_tilcdc_slave_compat_begin;
  119. static void *overlay_data;
  120. struct device_node *overlay;
  121. int ret;
  122. if (!size) {
  123. pr_warn("%s: No overlay data\n", __func__);
  124. return NULL;
  125. }
  126. overlay_data = kmemdup(__dtb_tilcdc_slave_compat_begin,
  127. size, GFP_KERNEL);
  128. if (!overlay_data || kfree_table_add(kft, overlay_data))
  129. return NULL;
  130. of_fdt_unflatten_tree(overlay_data, &overlay);
  131. if (!overlay) {
  132. pr_warn("%s: Unfattening overlay tree failed\n", __func__);
  133. return NULL;
  134. }
  135. of_node_set_flag(overlay, OF_DETACHED);
  136. ret = of_resolve_phandles(overlay);
  137. if (ret) {
  138. pr_err("%s: Failed to resolve phandles: %d\n", __func__, ret);
  139. return NULL;
  140. }
  141. return overlay;
  142. }
  143. static const struct of_device_id tilcdc_slave_of_match[] __initconst = {
  144. { .compatible = "ti,tilcdc,slave", },
  145. {},
  146. };
  147. static const struct of_device_id tilcdc_of_match[] __initconst = {
  148. { .compatible = "ti,am33xx-tilcdc", },
  149. {},
  150. };
  151. static const struct of_device_id tilcdc_tda998x_of_match[] __initconst = {
  152. { .compatible = "nxp,tda998x", },
  153. {},
  154. };
  155. static const char * const tilcdc_slave_props[] __initconst = {
  156. "pinctrl-names",
  157. "pinctrl-0",
  158. "pinctrl-1",
  159. NULL
  160. };
  161. void __init tilcdc_convert_slave_node(void)
  162. {
  163. struct device_node *slave = NULL, *lcdc = NULL;
  164. struct device_node *i2c = NULL, *fragment = NULL;
  165. struct device_node *overlay, *encoder;
  166. struct property *prop;
  167. /* For all memory needed for the overlay tree. This memory can
  168. be freed after the overlay has been applied. */
  169. struct kfree_table kft;
  170. int ret;
  171. if (kfree_table_init(&kft))
  172. goto out;
  173. lcdc = of_find_matching_node(NULL, tilcdc_of_match);
  174. slave = of_find_matching_node(NULL, tilcdc_slave_of_match);
  175. if (!slave || !of_device_is_available(lcdc))
  176. goto out;
  177. i2c = of_parse_phandle(slave, "i2c", 0);
  178. if (!i2c) {
  179. pr_err("%s: Can't find i2c node trough phandle\n", __func__);
  180. goto out;
  181. }
  182. overlay = tilcdc_get_overlay(&kft);
  183. if (!overlay)
  184. goto out;
  185. encoder = of_find_matching_node(overlay, tilcdc_tda998x_of_match);
  186. if (!encoder) {
  187. pr_err("%s: Failed to find tda998x node\n", __func__);
  188. goto out;
  189. }
  190. tilcdc_copy_props(slave, encoder, tilcdc_slave_props, &kft);
  191. for_each_child_of_node(overlay, fragment) {
  192. prop = of_find_property(fragment, "target-path", NULL);
  193. if (!prop)
  194. continue;
  195. if (!strncmp("i2c", (char *)prop->value, prop->length))
  196. if (tilcdc_prop_str_update(prop, i2c->full_name, &kft))
  197. goto out;
  198. if (!strncmp("lcdc", (char *)prop->value, prop->length))
  199. if (tilcdc_prop_str_update(prop, lcdc->full_name, &kft))
  200. goto out;
  201. }
  202. tilcdc_node_disable(slave);
  203. ret = of_overlay_create(overlay);
  204. if (ret)
  205. pr_err("%s: Creating overlay failed: %d\n", __func__, ret);
  206. else
  207. pr_info("%s: ti,tilcdc,slave node successfully converted\n",
  208. __func__);
  209. out:
  210. kfree_table_free(&kft);
  211. of_node_put(i2c);
  212. of_node_put(slave);
  213. of_node_put(lcdc);
  214. of_node_put(fragment);
  215. }
  216. int __init tilcdc_slave_compat_init(void)
  217. {
  218. tilcdc_convert_slave_node();
  219. return 0;
  220. }
  221. subsys_initcall(tilcdc_slave_compat_init);