tilcdc_slave_compat.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. static 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, NULL, &overlay);
  131. if (!overlay) {
  132. pr_warn("%s: Unfattening overlay tree failed\n", __func__);
  133. return NULL;
  134. }
  135. ret = of_resolve_phandles(overlay);
  136. if (ret) {
  137. pr_err("%s: Failed to resolve phandles: %d\n", __func__, ret);
  138. return NULL;
  139. }
  140. return overlay;
  141. }
  142. static const struct of_device_id tilcdc_slave_of_match[] __initconst = {
  143. { .compatible = "ti,tilcdc,slave", },
  144. {},
  145. };
  146. static const struct of_device_id tilcdc_of_match[] __initconst = {
  147. { .compatible = "ti,am33xx-tilcdc", },
  148. {},
  149. };
  150. static const struct of_device_id tilcdc_tda998x_of_match[] __initconst = {
  151. { .compatible = "nxp,tda998x", },
  152. {},
  153. };
  154. static const char * const tilcdc_slave_props[] __initconst = {
  155. "pinctrl-names",
  156. "pinctrl-0",
  157. "pinctrl-1",
  158. NULL
  159. };
  160. static void __init tilcdc_convert_slave_node(void)
  161. {
  162. struct device_node *slave = NULL, *lcdc = NULL;
  163. struct device_node *i2c = NULL, *fragment = NULL;
  164. struct device_node *overlay, *encoder;
  165. struct property *prop;
  166. /* For all memory needed for the overlay tree. This memory can
  167. be freed after the overlay has been applied. */
  168. struct kfree_table kft;
  169. int ret;
  170. if (kfree_table_init(&kft))
  171. return;
  172. lcdc = of_find_matching_node(NULL, tilcdc_of_match);
  173. slave = of_find_matching_node(NULL, tilcdc_slave_of_match);
  174. if (!slave || !of_device_is_available(lcdc))
  175. goto out;
  176. i2c = of_parse_phandle(slave, "i2c", 0);
  177. if (!i2c) {
  178. pr_err("%s: Can't find i2c node trough phandle\n", __func__);
  179. goto out;
  180. }
  181. overlay = tilcdc_get_overlay(&kft);
  182. if (!overlay)
  183. goto out;
  184. encoder = of_find_matching_node(overlay, tilcdc_tda998x_of_match);
  185. if (!encoder) {
  186. pr_err("%s: Failed to find tda998x node\n", __func__);
  187. goto out;
  188. }
  189. tilcdc_copy_props(slave, encoder, tilcdc_slave_props, &kft);
  190. for_each_child_of_node(overlay, fragment) {
  191. prop = of_find_property(fragment, "target-path", NULL);
  192. if (!prop)
  193. continue;
  194. if (!strncmp("i2c", (char *)prop->value, prop->length))
  195. if (tilcdc_prop_str_update(prop, i2c->full_name, &kft))
  196. goto out;
  197. if (!strncmp("lcdc", (char *)prop->value, prop->length))
  198. if (tilcdc_prop_str_update(prop, lcdc->full_name, &kft))
  199. goto out;
  200. }
  201. tilcdc_node_disable(slave);
  202. ret = of_overlay_create(overlay);
  203. if (ret)
  204. pr_err("%s: Creating overlay failed: %d\n", __func__, ret);
  205. else
  206. pr_info("%s: ti,tilcdc,slave node successfully converted\n",
  207. __func__);
  208. out:
  209. kfree_table_free(&kft);
  210. of_node_put(i2c);
  211. of_node_put(slave);
  212. of_node_put(lcdc);
  213. of_node_put(fragment);
  214. }
  215. static int __init tilcdc_slave_compat_init(void)
  216. {
  217. tilcdc_convert_slave_node();
  218. return 0;
  219. }
  220. subsys_initcall(tilcdc_slave_compat_init);