xref: /linux/drivers/gpu/drm/bridge/imx/imx8qxp-pxl2dpi.c (revision 6f7e6393d1ce636bb7ec77a7fe7b77458fddf701)
1 // SPDX-License-Identifier: GPL-2.0+
2 
3 /*
4  * Copyright 2020 NXP
5  */
6 
7 #include <linux/firmware/imx/svc/misc.h>
8 #include <linux/media-bus-format.h>
9 #include <linux/mfd/syscon.h>
10 #include <linux/module.h>
11 #include <linux/of.h>
12 #include <linux/of_device.h>
13 #include <linux/of_graph.h>
14 #include <linux/platform_device.h>
15 #include <linux/pm_runtime.h>
16 #include <linux/regmap.h>
17 
18 #include <drm/drm_atomic_state_helper.h>
19 #include <drm/drm_bridge.h>
20 #include <drm/drm_of.h>
21 #include <drm/drm_print.h>
22 
23 #include <dt-bindings/firmware/imx/rsrc.h>
24 
25 #define PXL2DPI_CTRL	0x40
26 #define  CFG1_16BIT	0x0
27 #define  CFG2_16BIT	0x1
28 #define  CFG3_16BIT	0x2
29 #define  CFG1_18BIT	0x3
30 #define  CFG2_18BIT	0x4
31 #define  CFG_24BIT	0x5
32 
33 #define DRIVER_NAME	"imx8qxp-pxl2dpi"
34 
35 struct imx8qxp_pxl2dpi {
36 	struct regmap *regmap;
37 	struct drm_bridge bridge;
38 	struct drm_bridge *next_bridge;
39 	struct drm_bridge *companion;
40 	struct device *dev;
41 	struct imx_sc_ipc *ipc_handle;
42 	u32 sc_resource;
43 	u32 in_bus_format;
44 	u32 out_bus_format;
45 	u32 pl_sel;
46 };
47 
48 #define bridge_to_p2d(b)	container_of(b, struct imx8qxp_pxl2dpi, bridge)
49 
50 static int imx8qxp_pxl2dpi_bridge_attach(struct drm_bridge *bridge,
51 					 struct drm_encoder *encoder,
52 					 enum drm_bridge_attach_flags flags)
53 {
54 	struct imx8qxp_pxl2dpi *p2d = bridge->driver_private;
55 
56 	if (!(flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)) {
57 		DRM_DEV_ERROR(p2d->dev,
58 			      "do not support creating a drm_connector\n");
59 		return -EINVAL;
60 	}
61 
62 	return drm_bridge_attach(encoder,
63 				 p2d->next_bridge, bridge,
64 				 DRM_BRIDGE_ATTACH_NO_CONNECTOR);
65 }
66 
67 static int
68 imx8qxp_pxl2dpi_bridge_atomic_check(struct drm_bridge *bridge,
69 				    struct drm_bridge_state *bridge_state,
70 				    struct drm_crtc_state *crtc_state,
71 				    struct drm_connector_state *conn_state)
72 {
73 	struct imx8qxp_pxl2dpi *p2d = bridge->driver_private;
74 
75 	p2d->in_bus_format = bridge_state->input_bus_cfg.format;
76 	p2d->out_bus_format = bridge_state->output_bus_cfg.format;
77 
78 	return 0;
79 }
80 
81 static void
82 imx8qxp_pxl2dpi_bridge_mode_set(struct drm_bridge *bridge,
83 				const struct drm_display_mode *mode,
84 				const struct drm_display_mode *adjusted_mode)
85 {
86 	struct imx8qxp_pxl2dpi *p2d = bridge->driver_private;
87 	struct imx8qxp_pxl2dpi *companion_p2d;
88 	int ret;
89 
90 	ret = pm_runtime_get_sync(p2d->dev);
91 	if (ret < 0)
92 		DRM_DEV_ERROR(p2d->dev,
93 			      "failed to get runtime PM sync: %d\n", ret);
94 
95 	ret = imx_sc_misc_set_control(p2d->ipc_handle, p2d->sc_resource,
96 				      IMX_SC_C_PXL_LINK_SEL, p2d->pl_sel);
97 	if (ret)
98 		DRM_DEV_ERROR(p2d->dev,
99 			      "failed to set pixel link selection(%u): %d\n",
100 							p2d->pl_sel, ret);
101 
102 	switch (p2d->out_bus_format) {
103 	case MEDIA_BUS_FMT_RGB888_1X24:
104 		regmap_write(p2d->regmap, PXL2DPI_CTRL, CFG_24BIT);
105 		break;
106 	case MEDIA_BUS_FMT_RGB666_1X24_CPADHI:
107 		regmap_write(p2d->regmap, PXL2DPI_CTRL, CFG2_18BIT);
108 		break;
109 	default:
110 		DRM_DEV_ERROR(p2d->dev,
111 			      "unsupported output bus format 0x%08x\n",
112 							p2d->out_bus_format);
113 	}
114 
115 	if (p2d->companion) {
116 		companion_p2d = bridge_to_p2d(p2d->companion);
117 
118 		companion_p2d->in_bus_format = p2d->in_bus_format;
119 		companion_p2d->out_bus_format = p2d->out_bus_format;
120 
121 		p2d->companion->funcs->mode_set(p2d->companion, mode,
122 							adjusted_mode);
123 	}
124 }
125 
126 static void imx8qxp_pxl2dpi_bridge_atomic_disable(struct drm_bridge *bridge,
127 						  struct drm_atomic_state *state)
128 {
129 	struct imx8qxp_pxl2dpi *p2d = bridge->driver_private;
130 
131 	pm_runtime_put(p2d->dev);
132 
133 	if (p2d->companion)
134 		p2d->companion->funcs->atomic_disable(p2d->companion, state);
135 }
136 
137 static const u32 imx8qxp_pxl2dpi_bus_output_fmts[] = {
138 	MEDIA_BUS_FMT_RGB888_1X24,
139 	MEDIA_BUS_FMT_RGB666_1X24_CPADHI,
140 };
141 
142 static bool imx8qxp_pxl2dpi_bus_output_fmt_supported(u32 fmt)
143 {
144 	int i;
145 
146 	for (i = 0; i < ARRAY_SIZE(imx8qxp_pxl2dpi_bus_output_fmts); i++) {
147 		if (imx8qxp_pxl2dpi_bus_output_fmts[i] == fmt)
148 			return true;
149 	}
150 
151 	return false;
152 }
153 
154 static u32 *
155 imx8qxp_pxl2dpi_bridge_atomic_get_input_bus_fmts(struct drm_bridge *bridge,
156 						 struct drm_bridge_state *bridge_state,
157 						 struct drm_crtc_state *crtc_state,
158 						 struct drm_connector_state *conn_state,
159 						 u32 output_fmt,
160 						 unsigned int *num_input_fmts)
161 {
162 	u32 *input_fmts;
163 
164 	if (!imx8qxp_pxl2dpi_bus_output_fmt_supported(output_fmt))
165 		return NULL;
166 
167 	*num_input_fmts = 1;
168 
169 	input_fmts = kmalloc(sizeof(*input_fmts), GFP_KERNEL);
170 	if (!input_fmts)
171 		return NULL;
172 
173 	switch (output_fmt) {
174 	case MEDIA_BUS_FMT_RGB888_1X24:
175 		input_fmts[0] = MEDIA_BUS_FMT_RGB888_1X36_CPADLO;
176 		break;
177 	case MEDIA_BUS_FMT_RGB666_1X24_CPADHI:
178 		input_fmts[0] = MEDIA_BUS_FMT_RGB666_1X36_CPADLO;
179 		break;
180 	default:
181 		kfree(input_fmts);
182 		input_fmts = NULL;
183 		break;
184 	}
185 
186 	return input_fmts;
187 }
188 
189 static u32 *
190 imx8qxp_pxl2dpi_bridge_atomic_get_output_bus_fmts(struct drm_bridge *bridge,
191 						  struct drm_bridge_state *bridge_state,
192 						  struct drm_crtc_state *crtc_state,
193 						  struct drm_connector_state *conn_state,
194 						  unsigned int *num_output_fmts)
195 {
196 	*num_output_fmts = ARRAY_SIZE(imx8qxp_pxl2dpi_bus_output_fmts);
197 	return kmemdup(imx8qxp_pxl2dpi_bus_output_fmts,
198 			sizeof(imx8qxp_pxl2dpi_bus_output_fmts), GFP_KERNEL);
199 }
200 
201 static const struct drm_bridge_funcs imx8qxp_pxl2dpi_bridge_funcs = {
202 	.atomic_duplicate_state	= drm_atomic_helper_bridge_duplicate_state,
203 	.atomic_destroy_state	= drm_atomic_helper_bridge_destroy_state,
204 	.atomic_reset		= drm_atomic_helper_bridge_reset,
205 	.attach			= imx8qxp_pxl2dpi_bridge_attach,
206 	.atomic_check		= imx8qxp_pxl2dpi_bridge_atomic_check,
207 	.mode_set		= imx8qxp_pxl2dpi_bridge_mode_set,
208 	.atomic_disable		= imx8qxp_pxl2dpi_bridge_atomic_disable,
209 	.atomic_get_input_bus_fmts =
210 			imx8qxp_pxl2dpi_bridge_atomic_get_input_bus_fmts,
211 	.atomic_get_output_bus_fmts =
212 			imx8qxp_pxl2dpi_bridge_atomic_get_output_bus_fmts,
213 };
214 
215 static struct device_node *
216 imx8qxp_pxl2dpi_get_available_ep_from_port(struct imx8qxp_pxl2dpi *p2d,
217 					   u32 port_id)
218 {
219 	struct device_node *port, *ep;
220 	int ep_cnt;
221 
222 	port = of_graph_get_port_by_id(p2d->dev->of_node, port_id);
223 	if (!port) {
224 		DRM_DEV_ERROR(p2d->dev, "failed to get port@%u\n", port_id);
225 		return ERR_PTR(-ENODEV);
226 	}
227 
228 	ep_cnt = of_get_available_child_count(port);
229 	if (ep_cnt == 0) {
230 		DRM_DEV_ERROR(p2d->dev, "no available endpoints of port@%u\n",
231 			      port_id);
232 		ep = ERR_PTR(-ENODEV);
233 		goto out;
234 	} else if (ep_cnt > 1) {
235 		DRM_DEV_ERROR(p2d->dev,
236 			      "invalid available endpoints of port@%u\n",
237 			      port_id);
238 		ep = ERR_PTR(-EINVAL);
239 		goto out;
240 	}
241 
242 	ep = of_get_next_available_child(port, NULL);
243 	if (!ep) {
244 		DRM_DEV_ERROR(p2d->dev,
245 			      "failed to get available endpoint of port@%u\n",
246 			      port_id);
247 		ep = ERR_PTR(-ENODEV);
248 		goto out;
249 	}
250 out:
251 	of_node_put(port);
252 	return ep;
253 }
254 
255 static struct drm_bridge *
256 imx8qxp_pxl2dpi_find_next_bridge(struct imx8qxp_pxl2dpi *p2d)
257 {
258 	struct device_node *ep, *remote;
259 	struct drm_bridge *next_bridge;
260 	int ret;
261 
262 	ep = imx8qxp_pxl2dpi_get_available_ep_from_port(p2d, 1);
263 	if (IS_ERR(ep)) {
264 		ret = PTR_ERR(ep);
265 		return ERR_PTR(ret);
266 	}
267 
268 	remote = of_graph_get_remote_port_parent(ep);
269 	if (!remote || !of_device_is_available(remote)) {
270 		DRM_DEV_ERROR(p2d->dev, "no available remote\n");
271 		next_bridge = ERR_PTR(-ENODEV);
272 		goto out;
273 	} else if (!of_device_is_available(remote->parent)) {
274 		DRM_DEV_ERROR(p2d->dev, "remote parent is not available\n");
275 		next_bridge = ERR_PTR(-ENODEV);
276 		goto out;
277 	}
278 
279 	next_bridge = of_drm_find_bridge(remote);
280 	if (!next_bridge) {
281 		next_bridge = ERR_PTR(-EPROBE_DEFER);
282 		goto out;
283 	}
284 out:
285 	of_node_put(remote);
286 	of_node_put(ep);
287 
288 	return next_bridge;
289 }
290 
291 static int imx8qxp_pxl2dpi_set_pixel_link_sel(struct imx8qxp_pxl2dpi *p2d)
292 {
293 	struct device_node *ep;
294 	struct of_endpoint endpoint;
295 	int ret;
296 
297 	ep = imx8qxp_pxl2dpi_get_available_ep_from_port(p2d, 0);
298 	if (IS_ERR(ep))
299 		return PTR_ERR(ep);
300 
301 	ret = of_graph_parse_endpoint(ep, &endpoint);
302 	if (ret) {
303 		DRM_DEV_ERROR(p2d->dev,
304 			      "failed to parse endpoint of port@0: %d\n", ret);
305 		goto out;
306 	}
307 
308 	p2d->pl_sel = endpoint.id;
309 out:
310 	of_node_put(ep);
311 
312 	return ret;
313 }
314 
315 static int imx8qxp_pxl2dpi_parse_dt_companion(struct imx8qxp_pxl2dpi *p2d)
316 {
317 	struct imx8qxp_pxl2dpi *companion_p2d;
318 	struct device *dev = p2d->dev;
319 	struct device_node *companion;
320 	struct device_node *port1, *port2;
321 	const struct of_device_id *match;
322 	int dual_link;
323 	int ret = 0;
324 
325 	/* Locate the companion PXL2DPI for dual-link operation, if any. */
326 	companion = of_parse_phandle(dev->of_node, "fsl,companion-pxl2dpi", 0);
327 	if (!companion)
328 		return 0;
329 
330 	if (!of_device_is_available(companion)) {
331 		DRM_DEV_ERROR(dev, "companion PXL2DPI is not available\n");
332 		ret = -ENODEV;
333 		goto out;
334 	}
335 
336 	/*
337 	 * Sanity check: the companion bridge must have the same compatible
338 	 * string.
339 	 */
340 	match = of_match_device(dev->driver->of_match_table, dev);
341 	if (!of_device_is_compatible(companion, match->compatible)) {
342 		DRM_DEV_ERROR(dev, "companion PXL2DPI is incompatible\n");
343 		ret = -ENXIO;
344 		goto out;
345 	}
346 
347 	p2d->companion = of_drm_find_bridge(companion);
348 	if (!p2d->companion) {
349 		ret = -EPROBE_DEFER;
350 		DRM_DEV_DEBUG_DRIVER(p2d->dev,
351 				     "failed to find companion bridge: %d\n",
352 				     ret);
353 		goto out;
354 	}
355 
356 	companion_p2d = bridge_to_p2d(p2d->companion);
357 
358 	/*
359 	 * We need to work out if the sink is expecting us to function in
360 	 * dual-link mode.  We do this by looking at the DT port nodes that
361 	 * the next bridges are connected to.  If they are marked as expecting
362 	 * even pixels and odd pixels than we need to use the companion PXL2DPI.
363 	 */
364 	port1 = of_graph_get_port_by_id(p2d->next_bridge->of_node, 1);
365 	port2 = of_graph_get_port_by_id(companion_p2d->next_bridge->of_node, 1);
366 	dual_link = drm_of_lvds_get_dual_link_pixel_order(port1, port2);
367 	of_node_put(port1);
368 	of_node_put(port2);
369 
370 	if (dual_link < 0) {
371 		ret = dual_link;
372 		DRM_DEV_ERROR(dev, "failed to get dual link pixel order: %d\n",
373 			      ret);
374 		goto out;
375 	}
376 
377 	DRM_DEV_DEBUG_DRIVER(dev,
378 			     "dual-link configuration detected (companion bridge %pOF)\n",
379 			     companion);
380 out:
381 	of_node_put(companion);
382 	return ret;
383 }
384 
385 static int imx8qxp_pxl2dpi_bridge_probe(struct platform_device *pdev)
386 {
387 	struct imx8qxp_pxl2dpi *p2d;
388 	struct device *dev = &pdev->dev;
389 	struct device_node *np = dev->of_node;
390 	int ret;
391 
392 	p2d = devm_drm_bridge_alloc(dev, struct imx8qxp_pxl2dpi, bridge,
393 				    &imx8qxp_pxl2dpi_bridge_funcs);
394 	if (IS_ERR(p2d))
395 		return PTR_ERR(p2d);
396 
397 	p2d->regmap = syscon_node_to_regmap(np->parent);
398 	if (IS_ERR(p2d->regmap)) {
399 		ret = PTR_ERR(p2d->regmap);
400 		if (ret != -EPROBE_DEFER)
401 			DRM_DEV_ERROR(dev, "failed to get regmap: %d\n", ret);
402 		return ret;
403 	}
404 
405 	ret = imx_scu_get_handle(&p2d->ipc_handle);
406 	if (ret) {
407 		if (ret != -EPROBE_DEFER)
408 			DRM_DEV_ERROR(dev, "failed to get SCU ipc handle: %d\n",
409 				      ret);
410 		return ret;
411 	}
412 
413 	p2d->dev = dev;
414 
415 	ret = of_property_read_u32(np, "fsl,sc-resource", &p2d->sc_resource);
416 	if (ret) {
417 		DRM_DEV_ERROR(dev, "failed to get SC resource %d\n", ret);
418 		return ret;
419 	}
420 
421 	p2d->next_bridge = imx8qxp_pxl2dpi_find_next_bridge(p2d);
422 	if (IS_ERR(p2d->next_bridge)) {
423 		ret = PTR_ERR(p2d->next_bridge);
424 		if (ret != -EPROBE_DEFER)
425 			DRM_DEV_ERROR(dev, "failed to find next bridge: %d\n",
426 				      ret);
427 		return ret;
428 	}
429 
430 	ret = imx8qxp_pxl2dpi_set_pixel_link_sel(p2d);
431 	if (ret)
432 		return ret;
433 
434 	ret = imx8qxp_pxl2dpi_parse_dt_companion(p2d);
435 	if (ret)
436 		return ret;
437 
438 	platform_set_drvdata(pdev, p2d);
439 	pm_runtime_enable(dev);
440 
441 	p2d->bridge.driver_private = p2d;
442 	p2d->bridge.of_node = np;
443 
444 	drm_bridge_add(&p2d->bridge);
445 
446 	return ret;
447 }
448 
449 static void imx8qxp_pxl2dpi_bridge_remove(struct platform_device *pdev)
450 {
451 	struct imx8qxp_pxl2dpi *p2d = platform_get_drvdata(pdev);
452 
453 	drm_bridge_remove(&p2d->bridge);
454 
455 	pm_runtime_disable(&pdev->dev);
456 }
457 
458 static const struct of_device_id imx8qxp_pxl2dpi_dt_ids[] = {
459 	{ .compatible = "fsl,imx8qxp-pxl2dpi", },
460 	{ /* sentinel */ }
461 };
462 MODULE_DEVICE_TABLE(of, imx8qxp_pxl2dpi_dt_ids);
463 
464 static struct platform_driver imx8qxp_pxl2dpi_bridge_driver = {
465 	.probe	= imx8qxp_pxl2dpi_bridge_probe,
466 	.remove = imx8qxp_pxl2dpi_bridge_remove,
467 	.driver	= {
468 		.of_match_table = imx8qxp_pxl2dpi_dt_ids,
469 		.name = DRIVER_NAME,
470 	},
471 };
472 module_platform_driver(imx8qxp_pxl2dpi_bridge_driver);
473 
474 MODULE_DESCRIPTION("i.MX8QXP pixel link to DPI bridge driver");
475 MODULE_AUTHOR("Liu Ying <victor.liu@nxp.com>");
476 MODULE_LICENSE("GPL v2");
477 MODULE_ALIAS("platform:" DRIVER_NAME);
478