tilcdc_slave_compat.c 5.8 KB

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