]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/gpu/drm/tilcdc/tilcdc_slave_compat.c
mmc: core: prepend 0x to OCR entry in sysfs
[mirror_ubuntu-bionic-kernel.git] / drivers / gpu / drm / tilcdc / tilcdc_slave_compat.c
CommitLineData
4e722158
JS
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/*
12 * To support the old "ti,tilcdc,slave" binding the binding has to be
13 * transformed to the new external encoder binding.
14 */
15
16#include <linux/kernel.h>
17#include <linux/of.h>
18#include <linux/of_graph.h>
19#include <linux/of_fdt.h>
20#include <linux/slab.h>
21#include <linux/list.h>
22
23#include "tilcdc_slave_compat.h"
24
25struct kfree_table {
26 int total;
27 int num;
28 void **table;
29};
30
31static int __init kfree_table_init(struct kfree_table *kft)
32{
33 kft->total = 32;
34 kft->num = 0;
35 kft->table = kmalloc(kft->total * sizeof(*kft->table),
36 GFP_KERNEL);
37 if (!kft->table)
38 return -ENOMEM;
39
40 return 0;
41}
42
43static int __init kfree_table_add(struct kfree_table *kft, void *p)
44{
45 if (kft->num == kft->total) {
46 void **old = kft->table;
47
48 kft->total *= 2;
49 kft->table = krealloc(old, kft->total * sizeof(*kft->table),
50 GFP_KERNEL);
51 if (!kft->table) {
52 kft->table = old;
53 kfree(p);
54 return -ENOMEM;
55 }
56 }
57 kft->table[kft->num++] = p;
58 return 0;
59}
60
61static void __init kfree_table_free(struct kfree_table *kft)
62{
63 int i;
64
65 for (i = 0; i < kft->num; i++)
66 kfree(kft->table[i]);
67
68 kfree(kft->table);
69}
70
71static
72struct property * __init tilcdc_prop_dup(const struct property *prop,
73 struct kfree_table *kft)
74{
75 struct property *nprop;
76
77 nprop = kzalloc(sizeof(*nprop), GFP_KERNEL);
78 if (!nprop || kfree_table_add(kft, nprop))
79 return NULL;
80
81 nprop->name = kstrdup(prop->name, GFP_KERNEL);
82 if (!nprop->name || kfree_table_add(kft, nprop->name))
83 return NULL;
84
85 nprop->value = kmemdup(prop->value, prop->length, GFP_KERNEL);
86 if (!nprop->value || kfree_table_add(kft, nprop->value))
87 return NULL;
88
89 nprop->length = prop->length;
90
91 return nprop;
92}
93
94static void __init tilcdc_copy_props(struct device_node *from,
95 struct device_node *to,
96 const char * const props[],
97 struct kfree_table *kft)
98{
99 struct property *prop;
100 int i;
101
102 for (i = 0; props[i]; i++) {
103 prop = of_find_property(from, props[i], NULL);
104 if (!prop)
105 continue;
106
107 prop = tilcdc_prop_dup(prop, kft);
108 if (!prop)
109 continue;
110
111 prop->next = to->properties;
112 to->properties = prop;
113 }
114}
115
116static int __init tilcdc_prop_str_update(struct property *prop,
117 const char *str,
118 struct kfree_table *kft)
119{
120 prop->value = kstrdup(str, GFP_KERNEL);
121 if (kfree_table_add(kft, prop->value) || !prop->value)
122 return -ENOMEM;
123 prop->length = strlen(str)+1;
124 return 0;
125}
126
127static void __init tilcdc_node_disable(struct device_node *node)
128{
129 struct property *prop;
130
131 prop = kzalloc(sizeof(*prop), GFP_KERNEL);
132 if (!prop)
133 return;
134
135 prop->name = "status";
136 prop->value = "disabled";
137 prop->length = strlen((char *)prop->value)+1;
138
139 of_update_property(node, prop);
140}
141
952e8fae 142static struct device_node * __init tilcdc_get_overlay(struct kfree_table *kft)
4e722158
JS
143{
144 const int size = __dtb_tilcdc_slave_compat_end -
145 __dtb_tilcdc_slave_compat_begin;
146 static void *overlay_data;
147 struct device_node *overlay;
4e722158
JS
148
149 if (!size) {
150 pr_warn("%s: No overlay data\n", __func__);
151 return NULL;
152 }
153
154 overlay_data = kmemdup(__dtb_tilcdc_slave_compat_begin,
155 size, GFP_KERNEL);
156 if (!overlay_data || kfree_table_add(kft, overlay_data))
157 return NULL;
158
c4263233 159 of_fdt_unflatten_tree(overlay_data, NULL, &overlay);
4e722158
JS
160 if (!overlay) {
161 pr_warn("%s: Unfattening overlay tree failed\n", __func__);
162 return NULL;
163 }
164
4e722158
JS
165 return overlay;
166}
167
168static const struct of_device_id tilcdc_slave_of_match[] __initconst = {
169 { .compatible = "ti,tilcdc,slave", },
170 {},
171};
172
173static const struct of_device_id tilcdc_of_match[] __initconst = {
174 { .compatible = "ti,am33xx-tilcdc", },
175 {},
176};
177
178static const struct of_device_id tilcdc_tda998x_of_match[] __initconst = {
179 { .compatible = "nxp,tda998x", },
180 {},
181};
182
183static const char * const tilcdc_slave_props[] __initconst = {
184 "pinctrl-names",
185 "pinctrl-0",
186 "pinctrl-1",
187 NULL
188};
189
952e8fae 190static void __init tilcdc_convert_slave_node(void)
4e722158
JS
191{
192 struct device_node *slave = NULL, *lcdc = NULL;
193 struct device_node *i2c = NULL, *fragment = NULL;
194 struct device_node *overlay, *encoder;
195 struct property *prop;
196 /* For all memory needed for the overlay tree. This memory can
197 be freed after the overlay has been applied. */
198 struct kfree_table kft;
24789c5c 199 int ovcs_id, ret;
4e722158
JS
200
201 if (kfree_table_init(&kft))
cf97ee19 202 return;
4e722158
JS
203
204 lcdc = of_find_matching_node(NULL, tilcdc_of_match);
205 slave = of_find_matching_node(NULL, tilcdc_slave_of_match);
206
207 if (!slave || !of_device_is_available(lcdc))
208 goto out;
209
210 i2c = of_parse_phandle(slave, "i2c", 0);
211 if (!i2c) {
212 pr_err("%s: Can't find i2c node trough phandle\n", __func__);
213 goto out;
214 }
215
216 overlay = tilcdc_get_overlay(&kft);
217 if (!overlay)
218 goto out;
219
220 encoder = of_find_matching_node(overlay, tilcdc_tda998x_of_match);
221 if (!encoder) {
222 pr_err("%s: Failed to find tda998x node\n", __func__);
223 goto out;
224 }
225
226 tilcdc_copy_props(slave, encoder, tilcdc_slave_props, &kft);
227
228 for_each_child_of_node(overlay, fragment) {
229 prop = of_find_property(fragment, "target-path", NULL);
230 if (!prop)
231 continue;
232 if (!strncmp("i2c", (char *)prop->value, prop->length))
233 if (tilcdc_prop_str_update(prop, i2c->full_name, &kft))
234 goto out;
235 if (!strncmp("lcdc", (char *)prop->value, prop->length))
236 if (tilcdc_prop_str_update(prop, lcdc->full_name, &kft))
237 goto out;
238 }
239
240 tilcdc_node_disable(slave);
241
24789c5c
FR
242 ovcs_id = 0;
243 ret = of_overlay_apply(overlay, &ovcs_id);
4e722158 244 if (ret)
0290c4ca
FR
245 pr_err("%s: Applying overlay changeset failed: %d\n",
246 __func__, ret);
4e722158
JS
247 else
248 pr_info("%s: ti,tilcdc,slave node successfully converted\n",
249 __func__);
250out:
251 kfree_table_free(&kft);
252 of_node_put(i2c);
253 of_node_put(slave);
254 of_node_put(lcdc);
255 of_node_put(fragment);
256}
257
952e8fae 258static int __init tilcdc_slave_compat_init(void)
4e722158
JS
259{
260 tilcdc_convert_slave_node();
261 return 0;
262}
263
264subsys_initcall(tilcdc_slave_compat_init);