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