xref: /OK3568_Linux_fs/kernel/drivers/gpu/drm/rcar-du/rcar_du_of.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * rcar_du_of.c - Legacy DT bindings compatibility
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 2018 Laurent Pinchart <laurent.pinchart@ideasonboard.com>
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Based on work from Jyri Sarha <jsarha@ti.com>
8*4882a593Smuzhiyun  * Copyright (C) 2015 Texas Instruments
9*4882a593Smuzhiyun  */
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun #include <linux/init.h>
12*4882a593Smuzhiyun #include <linux/kernel.h>
13*4882a593Smuzhiyun #include <linux/of.h>
14*4882a593Smuzhiyun #include <linux/of_address.h>
15*4882a593Smuzhiyun #include <linux/of_fdt.h>
16*4882a593Smuzhiyun #include <linux/of_graph.h>
17*4882a593Smuzhiyun #include <linux/slab.h>
18*4882a593Smuzhiyun 
19*4882a593Smuzhiyun #include "rcar_du_crtc.h"
20*4882a593Smuzhiyun #include "rcar_du_drv.h"
21*4882a593Smuzhiyun #include "rcar_du_of.h"
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun /* -----------------------------------------------------------------------------
24*4882a593Smuzhiyun  * Generic Overlay Handling
25*4882a593Smuzhiyun  */
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun struct rcar_du_of_overlay {
28*4882a593Smuzhiyun 	const char *compatible;
29*4882a593Smuzhiyun 	void *begin;
30*4882a593Smuzhiyun 	void *end;
31*4882a593Smuzhiyun };
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun #define RCAR_DU_OF_DTB(type, soc)					\
34*4882a593Smuzhiyun 	extern char __dtb_rcar_du_of_##type##_##soc##_begin[];		\
35*4882a593Smuzhiyun 	extern char __dtb_rcar_du_of_##type##_##soc##_end[]
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun #define RCAR_DU_OF_OVERLAY(type, soc)					\
38*4882a593Smuzhiyun 	{								\
39*4882a593Smuzhiyun 		.compatible = "renesas,du-" #soc,			\
40*4882a593Smuzhiyun 		.begin = __dtb_rcar_du_of_##type##_##soc##_begin,	\
41*4882a593Smuzhiyun 		.end = __dtb_rcar_du_of_##type##_##soc##_end,		\
42*4882a593Smuzhiyun 	}
43*4882a593Smuzhiyun 
rcar_du_of_apply_overlay(const struct rcar_du_of_overlay * dtbs,const char * compatible)44*4882a593Smuzhiyun static int __init rcar_du_of_apply_overlay(const struct rcar_du_of_overlay *dtbs,
45*4882a593Smuzhiyun 					   const char *compatible)
46*4882a593Smuzhiyun {
47*4882a593Smuzhiyun 	const struct rcar_du_of_overlay *dtb = NULL;
48*4882a593Smuzhiyun 	unsigned int i;
49*4882a593Smuzhiyun 	int ovcs_id;
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun 	for (i = 0; dtbs[i].compatible; ++i) {
52*4882a593Smuzhiyun 		if (!strcmp(dtbs[i].compatible, compatible)) {
53*4882a593Smuzhiyun 			dtb = &dtbs[i];
54*4882a593Smuzhiyun 			break;
55*4882a593Smuzhiyun 		}
56*4882a593Smuzhiyun 	}
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun 	if (!dtb)
59*4882a593Smuzhiyun 		return -ENODEV;
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun 	ovcs_id = 0;
62*4882a593Smuzhiyun 	return of_overlay_fdt_apply(dtb->begin, dtb->end - dtb->begin,
63*4882a593Smuzhiyun 				    &ovcs_id);
64*4882a593Smuzhiyun }
65*4882a593Smuzhiyun 
rcar_du_of_add_property(struct of_changeset * ocs,struct device_node * np,const char * name,const void * value,int length)66*4882a593Smuzhiyun static int __init rcar_du_of_add_property(struct of_changeset *ocs,
67*4882a593Smuzhiyun 					  struct device_node *np,
68*4882a593Smuzhiyun 					  const char *name, const void *value,
69*4882a593Smuzhiyun 					  int length)
70*4882a593Smuzhiyun {
71*4882a593Smuzhiyun 	struct property *prop;
72*4882a593Smuzhiyun 	int ret = -ENOMEM;
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun 	prop = kzalloc(sizeof(*prop), GFP_KERNEL);
75*4882a593Smuzhiyun 	if (!prop)
76*4882a593Smuzhiyun 		return -ENOMEM;
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun 	prop->name = kstrdup(name, GFP_KERNEL);
79*4882a593Smuzhiyun 	if (!prop->name)
80*4882a593Smuzhiyun 		goto out_err;
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun 	prop->value = kmemdup(value, length, GFP_KERNEL);
83*4882a593Smuzhiyun 	if (!prop->value)
84*4882a593Smuzhiyun 		goto out_err;
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun 	of_property_set_flag(prop, OF_DYNAMIC);
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun 	prop->length = length;
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun 	ret = of_changeset_add_property(ocs, np, prop);
91*4882a593Smuzhiyun 	if (!ret)
92*4882a593Smuzhiyun 		return 0;
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun out_err:
95*4882a593Smuzhiyun 	kfree(prop->value);
96*4882a593Smuzhiyun 	kfree(prop->name);
97*4882a593Smuzhiyun 	kfree(prop);
98*4882a593Smuzhiyun 	return ret;
99*4882a593Smuzhiyun }
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun /* -----------------------------------------------------------------------------
102*4882a593Smuzhiyun  * LVDS Overlays
103*4882a593Smuzhiyun  */
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun RCAR_DU_OF_DTB(lvds, r8a7790);
106*4882a593Smuzhiyun RCAR_DU_OF_DTB(lvds, r8a7791);
107*4882a593Smuzhiyun RCAR_DU_OF_DTB(lvds, r8a7793);
108*4882a593Smuzhiyun RCAR_DU_OF_DTB(lvds, r8a7795);
109*4882a593Smuzhiyun RCAR_DU_OF_DTB(lvds, r8a7796);
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun static const struct rcar_du_of_overlay rcar_du_lvds_overlays[] __initconst = {
112*4882a593Smuzhiyun 	RCAR_DU_OF_OVERLAY(lvds, r8a7790),
113*4882a593Smuzhiyun 	RCAR_DU_OF_OVERLAY(lvds, r8a7791),
114*4882a593Smuzhiyun 	RCAR_DU_OF_OVERLAY(lvds, r8a7793),
115*4882a593Smuzhiyun 	RCAR_DU_OF_OVERLAY(lvds, r8a7795),
116*4882a593Smuzhiyun 	RCAR_DU_OF_OVERLAY(lvds, r8a7796),
117*4882a593Smuzhiyun 	{ /* Sentinel */ },
118*4882a593Smuzhiyun };
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun static struct of_changeset rcar_du_lvds_changeset;
121*4882a593Smuzhiyun 
rcar_du_of_lvds_patch_one(struct device_node * lvds,const struct of_phandle_args * clk,struct device_node * local,struct device_node * remote)122*4882a593Smuzhiyun static void __init rcar_du_of_lvds_patch_one(struct device_node *lvds,
123*4882a593Smuzhiyun 					     const struct of_phandle_args *clk,
124*4882a593Smuzhiyun 					     struct device_node *local,
125*4882a593Smuzhiyun 					     struct device_node *remote)
126*4882a593Smuzhiyun {
127*4882a593Smuzhiyun 	unsigned int psize;
128*4882a593Smuzhiyun 	unsigned int i;
129*4882a593Smuzhiyun 	__be32 value[4];
130*4882a593Smuzhiyun 	int ret;
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun 	/*
133*4882a593Smuzhiyun 	 * Set the LVDS clocks property. This can't be performed by the overlay
134*4882a593Smuzhiyun 	 * as the structure of the clock specifier has changed over time, and we
135*4882a593Smuzhiyun 	 * don't know at compile time which binding version the system we will
136*4882a593Smuzhiyun 	 * run on uses.
137*4882a593Smuzhiyun 	 */
138*4882a593Smuzhiyun 	if (clk->args_count >= ARRAY_SIZE(value) - 1)
139*4882a593Smuzhiyun 		return;
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun 	of_changeset_init(&rcar_du_lvds_changeset);
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun 	value[0] = cpu_to_be32(clk->np->phandle);
144*4882a593Smuzhiyun 	for (i = 0; i < clk->args_count; ++i)
145*4882a593Smuzhiyun 		value[i + 1] = cpu_to_be32(clk->args[i]);
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun 	psize = (clk->args_count + 1) * 4;
148*4882a593Smuzhiyun 	ret = rcar_du_of_add_property(&rcar_du_lvds_changeset, lvds,
149*4882a593Smuzhiyun 				      "clocks", value, psize);
150*4882a593Smuzhiyun 	if (ret < 0)
151*4882a593Smuzhiyun 		goto done;
152*4882a593Smuzhiyun 
153*4882a593Smuzhiyun 	/*
154*4882a593Smuzhiyun 	 * Insert the node in the OF graph: patch the LVDS ports remote-endpoint
155*4882a593Smuzhiyun 	 * properties to point to the endpoints of the sibling nodes in the
156*4882a593Smuzhiyun 	 * graph. This can't be performed by the overlay: on the input side the
157*4882a593Smuzhiyun 	 * overlay would contain a phandle for the DU LVDS output port that
158*4882a593Smuzhiyun 	 * would clash with the system DT, and on the output side the connection
159*4882a593Smuzhiyun 	 * is board-specific.
160*4882a593Smuzhiyun 	 */
161*4882a593Smuzhiyun 	value[0] = cpu_to_be32(local->phandle);
162*4882a593Smuzhiyun 	value[1] = cpu_to_be32(remote->phandle);
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun 	for (i = 0; i < 2; ++i) {
165*4882a593Smuzhiyun 		struct device_node *endpoint;
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun 		endpoint = of_graph_get_endpoint_by_regs(lvds, i, 0);
168*4882a593Smuzhiyun 		if (!endpoint) {
169*4882a593Smuzhiyun 			ret = -EINVAL;
170*4882a593Smuzhiyun 			goto done;
171*4882a593Smuzhiyun 		}
172*4882a593Smuzhiyun 
173*4882a593Smuzhiyun 		ret = rcar_du_of_add_property(&rcar_du_lvds_changeset,
174*4882a593Smuzhiyun 					      endpoint, "remote-endpoint",
175*4882a593Smuzhiyun 					      &value[i], sizeof(value[i]));
176*4882a593Smuzhiyun 		of_node_put(endpoint);
177*4882a593Smuzhiyun 		if (ret < 0)
178*4882a593Smuzhiyun 			goto done;
179*4882a593Smuzhiyun 	}
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun 	ret = of_changeset_apply(&rcar_du_lvds_changeset);
182*4882a593Smuzhiyun 
183*4882a593Smuzhiyun done:
184*4882a593Smuzhiyun 	if (ret < 0)
185*4882a593Smuzhiyun 		of_changeset_destroy(&rcar_du_lvds_changeset);
186*4882a593Smuzhiyun }
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun struct lvds_of_data {
189*4882a593Smuzhiyun 	struct resource res;
190*4882a593Smuzhiyun 	struct of_phandle_args clkspec;
191*4882a593Smuzhiyun 	struct device_node *local;
192*4882a593Smuzhiyun 	struct device_node *remote;
193*4882a593Smuzhiyun };
194*4882a593Smuzhiyun 
rcar_du_of_lvds_patch(const struct of_device_id * of_ids)195*4882a593Smuzhiyun static void __init rcar_du_of_lvds_patch(const struct of_device_id *of_ids)
196*4882a593Smuzhiyun {
197*4882a593Smuzhiyun 	const struct rcar_du_device_info *info;
198*4882a593Smuzhiyun 	const struct of_device_id *match;
199*4882a593Smuzhiyun 	struct lvds_of_data lvds_data[2] = { };
200*4882a593Smuzhiyun 	struct device_node *lvds_node;
201*4882a593Smuzhiyun 	struct device_node *soc_node;
202*4882a593Smuzhiyun 	struct device_node *du_node;
203*4882a593Smuzhiyun 	char compatible[22];
204*4882a593Smuzhiyun 	const char *soc_name;
205*4882a593Smuzhiyun 	unsigned int i;
206*4882a593Smuzhiyun 	int ret;
207*4882a593Smuzhiyun 
208*4882a593Smuzhiyun 	/* Get the DU node and exit if not present or disabled. */
209*4882a593Smuzhiyun 	du_node = of_find_matching_node_and_match(NULL, of_ids, &match);
210*4882a593Smuzhiyun 	if (!du_node || !of_device_is_available(du_node)) {
211*4882a593Smuzhiyun 		of_node_put(du_node);
212*4882a593Smuzhiyun 		return;
213*4882a593Smuzhiyun 	}
214*4882a593Smuzhiyun 
215*4882a593Smuzhiyun 	info = match->data;
216*4882a593Smuzhiyun 	soc_node = of_get_parent(du_node);
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun 	if (WARN_ON(info->num_lvds > ARRAY_SIZE(lvds_data)))
219*4882a593Smuzhiyun 		goto done;
220*4882a593Smuzhiyun 
221*4882a593Smuzhiyun 	/*
222*4882a593Smuzhiyun 	 * Skip if the LVDS nodes already exists.
223*4882a593Smuzhiyun 	 *
224*4882a593Smuzhiyun 	 * The nodes are searched based on the compatible string, which we
225*4882a593Smuzhiyun 	 * construct from the SoC name found in the DU compatible string. As a
226*4882a593Smuzhiyun 	 * match has been found we know the compatible string matches the
227*4882a593Smuzhiyun 	 * expected format and can thus skip some of the string manipulation
228*4882a593Smuzhiyun 	 * normal safety checks.
229*4882a593Smuzhiyun 	 */
230*4882a593Smuzhiyun 	soc_name = strchr(match->compatible, '-') + 1;
231*4882a593Smuzhiyun 	sprintf(compatible, "renesas,%s-lvds", soc_name);
232*4882a593Smuzhiyun 	lvds_node = of_find_compatible_node(NULL, NULL, compatible);
233*4882a593Smuzhiyun 	if (lvds_node) {
234*4882a593Smuzhiyun 		of_node_put(lvds_node);
235*4882a593Smuzhiyun 		return;
236*4882a593Smuzhiyun 	}
237*4882a593Smuzhiyun 
238*4882a593Smuzhiyun 	/*
239*4882a593Smuzhiyun 	 * Parse the DU node and store the register specifier, the clock
240*4882a593Smuzhiyun 	 * specifier and the local and remote endpoint of the LVDS link for
241*4882a593Smuzhiyun 	 * later use.
242*4882a593Smuzhiyun 	 */
243*4882a593Smuzhiyun 	for (i = 0; i < info->num_lvds; ++i) {
244*4882a593Smuzhiyun 		struct lvds_of_data *lvds = &lvds_data[i];
245*4882a593Smuzhiyun 		unsigned int port;
246*4882a593Smuzhiyun 		char name[7];
247*4882a593Smuzhiyun 		int index;
248*4882a593Smuzhiyun 
249*4882a593Smuzhiyun 		sprintf(name, "lvds.%u", i);
250*4882a593Smuzhiyun 		index = of_property_match_string(du_node, "clock-names", name);
251*4882a593Smuzhiyun 		if (index < 0)
252*4882a593Smuzhiyun 			continue;
253*4882a593Smuzhiyun 
254*4882a593Smuzhiyun 		ret = of_parse_phandle_with_args(du_node, "clocks",
255*4882a593Smuzhiyun 						 "#clock-cells", index,
256*4882a593Smuzhiyun 						 &lvds->clkspec);
257*4882a593Smuzhiyun 		if (ret < 0)
258*4882a593Smuzhiyun 			continue;
259*4882a593Smuzhiyun 
260*4882a593Smuzhiyun 		port = info->routes[RCAR_DU_OUTPUT_LVDS0 + i].port;
261*4882a593Smuzhiyun 
262*4882a593Smuzhiyun 		lvds->local = of_graph_get_endpoint_by_regs(du_node, port, 0);
263*4882a593Smuzhiyun 		if (!lvds->local)
264*4882a593Smuzhiyun 			continue;
265*4882a593Smuzhiyun 
266*4882a593Smuzhiyun 		lvds->remote = of_graph_get_remote_endpoint(lvds->local);
267*4882a593Smuzhiyun 		if (!lvds->remote)
268*4882a593Smuzhiyun 			continue;
269*4882a593Smuzhiyun 
270*4882a593Smuzhiyun 		index = of_property_match_string(du_node, "reg-names", name);
271*4882a593Smuzhiyun 		if (index < 0)
272*4882a593Smuzhiyun 			continue;
273*4882a593Smuzhiyun 
274*4882a593Smuzhiyun 		of_address_to_resource(du_node, index, &lvds->res);
275*4882a593Smuzhiyun 	}
276*4882a593Smuzhiyun 
277*4882a593Smuzhiyun 	/* Parse and apply the overlay. This will resolve phandles. */
278*4882a593Smuzhiyun 	ret = rcar_du_of_apply_overlay(rcar_du_lvds_overlays,
279*4882a593Smuzhiyun 				       match->compatible);
280*4882a593Smuzhiyun 	if (ret < 0)
281*4882a593Smuzhiyun 		goto done;
282*4882a593Smuzhiyun 
283*4882a593Smuzhiyun 	/* Patch the newly created LVDS encoder nodes. */
284*4882a593Smuzhiyun 	for_each_child_of_node(soc_node, lvds_node) {
285*4882a593Smuzhiyun 		struct resource res;
286*4882a593Smuzhiyun 
287*4882a593Smuzhiyun 		if (!of_device_is_compatible(lvds_node, compatible))
288*4882a593Smuzhiyun 			continue;
289*4882a593Smuzhiyun 
290*4882a593Smuzhiyun 		/* Locate the lvds_data entry based on the resource start. */
291*4882a593Smuzhiyun 		ret = of_address_to_resource(lvds_node, 0, &res);
292*4882a593Smuzhiyun 		if (ret < 0)
293*4882a593Smuzhiyun 			continue;
294*4882a593Smuzhiyun 
295*4882a593Smuzhiyun 		for (i = 0; i < ARRAY_SIZE(lvds_data); ++i) {
296*4882a593Smuzhiyun 			if (lvds_data[i].res.start == res.start)
297*4882a593Smuzhiyun 				break;
298*4882a593Smuzhiyun 		}
299*4882a593Smuzhiyun 
300*4882a593Smuzhiyun 		if (i == ARRAY_SIZE(lvds_data))
301*4882a593Smuzhiyun 			continue;
302*4882a593Smuzhiyun 
303*4882a593Smuzhiyun 		/* Patch the LVDS encoder. */
304*4882a593Smuzhiyun 		rcar_du_of_lvds_patch_one(lvds_node, &lvds_data[i].clkspec,
305*4882a593Smuzhiyun 					  lvds_data[i].local,
306*4882a593Smuzhiyun 					  lvds_data[i].remote);
307*4882a593Smuzhiyun 	}
308*4882a593Smuzhiyun 
309*4882a593Smuzhiyun done:
310*4882a593Smuzhiyun 	for (i = 0; i < info->num_lvds; ++i) {
311*4882a593Smuzhiyun 		of_node_put(lvds_data[i].clkspec.np);
312*4882a593Smuzhiyun 		of_node_put(lvds_data[i].local);
313*4882a593Smuzhiyun 		of_node_put(lvds_data[i].remote);
314*4882a593Smuzhiyun 	}
315*4882a593Smuzhiyun 
316*4882a593Smuzhiyun 	of_node_put(soc_node);
317*4882a593Smuzhiyun 	of_node_put(du_node);
318*4882a593Smuzhiyun }
319*4882a593Smuzhiyun 
rcar_du_of_init(const struct of_device_id * of_ids)320*4882a593Smuzhiyun void __init rcar_du_of_init(const struct of_device_id *of_ids)
321*4882a593Smuzhiyun {
322*4882a593Smuzhiyun 	rcar_du_of_lvds_patch(of_ids);
323*4882a593Smuzhiyun }
324