File Index
Files and directories
Sign in
FILE

imx8qxp-pxl2dpi.c

File preview with syntax highlighting.

linux-6.19.14/drivers/gpu/drm/bridge/imx/imx8qxp-pxl2dpi.c C
  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
 35struct 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
 50static 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
 67static int
 68imx8qxp_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
 81static void
 82imx8qxp_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
126static 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	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, state);
138}
139
140static const u32 imx8qxp_pxl2dpi_bus_output_fmts[] = {
141	MEDIA_BUS_FMT_RGB888_1X24,
142	MEDIA_BUS_FMT_RGB666_1X24_CPADHI,
143};
144
145static bool imx8qxp_pxl2dpi_bus_output_fmt_supported(u32 fmt)
146{
147	int i;
148
149	for (i = 0; i < ARRAY_SIZE(imx8qxp_pxl2dpi_bus_output_fmts); i++) {
150		if (imx8qxp_pxl2dpi_bus_output_fmts[i] == fmt)
151			return true;
152	}
153
154	return false;
155}
156
157static u32 *
158imx8qxp_pxl2dpi_bridge_atomic_get_input_bus_fmts(struct drm_bridge *bridge,
159						 struct drm_bridge_state *bridge_state,
160						 struct drm_crtc_state *crtc_state,
161						 struct drm_connector_state *conn_state,
162						 u32 output_fmt,
163						 unsigned int *num_input_fmts)
164{
165	u32 *input_fmts;
166
167	if (!imx8qxp_pxl2dpi_bus_output_fmt_supported(output_fmt))
168		return NULL;
169
170	*num_input_fmts = 1;
171
172	input_fmts = kmalloc(sizeof(*input_fmts), GFP_KERNEL);
173	if (!input_fmts)
174		return NULL;
175
176	switch (output_fmt) {
177	case MEDIA_BUS_FMT_RGB888_1X24:
178		input_fmts[0] = MEDIA_BUS_FMT_RGB888_1X36_CPADLO;
179		break;
180	case MEDIA_BUS_FMT_RGB666_1X24_CPADHI:
181		input_fmts[0] = MEDIA_BUS_FMT_RGB666_1X36_CPADLO;
182		break;
183	default:
184		kfree(input_fmts);
185		input_fmts = NULL;
186		break;
187	}
188
189	return input_fmts;
190}
191
192static u32 *
193imx8qxp_pxl2dpi_bridge_atomic_get_output_bus_fmts(struct drm_bridge *bridge,
194						  struct drm_bridge_state *bridge_state,
195						  struct drm_crtc_state *crtc_state,
196						  struct drm_connector_state *conn_state,
197						  unsigned int *num_output_fmts)
198{
199	*num_output_fmts = ARRAY_SIZE(imx8qxp_pxl2dpi_bus_output_fmts);
200	return kmemdup(imx8qxp_pxl2dpi_bus_output_fmts,
201			sizeof(imx8qxp_pxl2dpi_bus_output_fmts), GFP_KERNEL);
202}
203
204static const struct drm_bridge_funcs imx8qxp_pxl2dpi_bridge_funcs = {
205	.atomic_duplicate_state	= drm_atomic_helper_bridge_duplicate_state,
206	.atomic_destroy_state	= drm_atomic_helper_bridge_destroy_state,
207	.atomic_reset		= drm_atomic_helper_bridge_reset,
208	.attach			= imx8qxp_pxl2dpi_bridge_attach,
209	.atomic_check		= imx8qxp_pxl2dpi_bridge_atomic_check,
210	.mode_set		= imx8qxp_pxl2dpi_bridge_mode_set,
211	.atomic_disable		= imx8qxp_pxl2dpi_bridge_atomic_disable,
212	.atomic_get_input_bus_fmts =
213			imx8qxp_pxl2dpi_bridge_atomic_get_input_bus_fmts,
214	.atomic_get_output_bus_fmts =
215			imx8qxp_pxl2dpi_bridge_atomic_get_output_bus_fmts,
216};
217
218static struct device_node *
219imx8qxp_pxl2dpi_get_available_ep_from_port(struct imx8qxp_pxl2dpi *p2d,
220					   u32 port_id)
221{
222	struct device_node *port, *ep;
223	int ep_cnt;
224
225	port = of_graph_get_port_by_id(p2d->dev->of_node, port_id);
226	if (!port) {
227		DRM_DEV_ERROR(p2d->dev, "failed to get port@%u\n", port_id);
228		return ERR_PTR(-ENODEV);
229	}
230
231	ep_cnt = of_get_available_child_count(port);
232	if (ep_cnt == 0) {
233		DRM_DEV_ERROR(p2d->dev, "no available endpoints of port@%u\n",
234			      port_id);
235		ep = ERR_PTR(-ENODEV);
236		goto out;
237	} else if (ep_cnt > 1) {
238		DRM_DEV_ERROR(p2d->dev,
239			      "invalid available endpoints of port@%u\n",
240			      port_id);
241		ep = ERR_PTR(-EINVAL);
242		goto out;
243	}
244
245	ep = of_get_next_available_child(port, NULL);
246	if (!ep) {
247		DRM_DEV_ERROR(p2d->dev,
248			      "failed to get available endpoint of port@%u\n",
249			      port_id);
250		ep = ERR_PTR(-ENODEV);
251		goto out;
252	}
253out:
254	of_node_put(port);
255	return ep;
256}
257
258static struct drm_bridge *
259imx8qxp_pxl2dpi_find_next_bridge(struct imx8qxp_pxl2dpi *p2d)
260{
261	struct device_node *ep, *remote;
262	struct drm_bridge *next_bridge;
263	int ret;
264
265	ep = imx8qxp_pxl2dpi_get_available_ep_from_port(p2d, 1);
266	if (IS_ERR(ep)) {
267		ret = PTR_ERR(ep);
268		return ERR_PTR(ret);
269	}
270
271	remote = of_graph_get_remote_port_parent(ep);
272	if (!remote || !of_device_is_available(remote)) {
273		DRM_DEV_ERROR(p2d->dev, "no available remote\n");
274		next_bridge = ERR_PTR(-ENODEV);
275		goto out;
276	} else if (!of_device_is_available(remote->parent)) {
277		DRM_DEV_ERROR(p2d->dev, "remote parent is not available\n");
278		next_bridge = ERR_PTR(-ENODEV);
279		goto out;
280	}
281
282	next_bridge = of_drm_find_bridge(remote);
283	if (!next_bridge) {
284		next_bridge = ERR_PTR(-EPROBE_DEFER);
285		goto out;
286	}
287out:
288	of_node_put(remote);
289	of_node_put(ep);
290
291	return next_bridge;
292}
293
294static int imx8qxp_pxl2dpi_set_pixel_link_sel(struct imx8qxp_pxl2dpi *p2d)
295{
296	struct device_node *ep;
297	struct of_endpoint endpoint;
298	int ret;
299
300	ep = imx8qxp_pxl2dpi_get_available_ep_from_port(p2d, 0);
301	if (IS_ERR(ep))
302		return PTR_ERR(ep);
303
304	ret = of_graph_parse_endpoint(ep, &endpoint);
305	if (ret) {
306		DRM_DEV_ERROR(p2d->dev,
307			      "failed to parse endpoint of port@0: %d\n", ret);
308		goto out;
309	}
310
311	p2d->pl_sel = endpoint.id;
312out:
313	of_node_put(ep);
314
315	return ret;
316}
317
318static int imx8qxp_pxl2dpi_parse_dt_companion(struct imx8qxp_pxl2dpi *p2d)
319{
320	struct imx8qxp_pxl2dpi *companion_p2d;
321	struct device *dev = p2d->dev;
322	struct device_node *companion;
323	struct device_node *port1, *port2;
324	const struct of_device_id *match;
325	int dual_link;
326	int ret = 0;
327
328	/* Locate the companion PXL2DPI for dual-link operation, if any. */
329	companion = of_parse_phandle(dev->of_node, "fsl,companion-pxl2dpi", 0);
330	if (!companion)
331		return 0;
332
333	if (!of_device_is_available(companion)) {
334		DRM_DEV_ERROR(dev, "companion PXL2DPI is not available\n");
335		ret = -ENODEV;
336		goto out;
337	}
338
339	/*
340	 * Sanity check: the companion bridge must have the same compatible
341	 * string.
342	 */
343	match = of_match_device(dev->driver->of_match_table, dev);
344	if (!of_device_is_compatible(companion, match->compatible)) {
345		DRM_DEV_ERROR(dev, "companion PXL2DPI is incompatible\n");
346		ret = -ENXIO;
347		goto out;
348	}
349
350	p2d->companion = of_drm_find_bridge(companion);
351	if (!p2d->companion) {
352		ret = -EPROBE_DEFER;
353		DRM_DEV_DEBUG_DRIVER(p2d->dev,
354				     "failed to find companion bridge: %d\n",
355				     ret);
356		goto out;
357	}
358
359	companion_p2d = bridge_to_p2d(p2d->companion);
360
361	/*
362	 * We need to work out if the sink is expecting us to function in
363	 * dual-link mode.  We do this by looking at the DT port nodes that
364	 * the next bridges are connected to.  If they are marked as expecting
365	 * even pixels and odd pixels than we need to use the companion PXL2DPI.
366	 */
367	port1 = of_graph_get_port_by_id(p2d->next_bridge->of_node, 1);
368	port2 = of_graph_get_port_by_id(companion_p2d->next_bridge->of_node, 1);
369	dual_link = drm_of_lvds_get_dual_link_pixel_order(port1, port2);
370	of_node_put(port1);
371	of_node_put(port2);
372
373	if (dual_link < 0) {
374		ret = dual_link;
375		DRM_DEV_ERROR(dev, "failed to get dual link pixel order: %d\n",
376			      ret);
377		goto out;
378	}
379
380	DRM_DEV_DEBUG_DRIVER(dev,
381			     "dual-link configuration detected (companion bridge %pOF)\n",
382			     companion);
383out:
384	of_node_put(companion);
385	return ret;
386}
387
388static int imx8qxp_pxl2dpi_bridge_probe(struct platform_device *pdev)
389{
390	struct imx8qxp_pxl2dpi *p2d;
391	struct device *dev = &pdev->dev;
392	struct device_node *np = dev->of_node;
393	int ret;
394
395	p2d = devm_drm_bridge_alloc(dev, struct imx8qxp_pxl2dpi, bridge,
396				    &imx8qxp_pxl2dpi_bridge_funcs);
397	if (IS_ERR(p2d))
398		return PTR_ERR(p2d);
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.of_node = np;
446
447	drm_bridge_add(&p2d->bridge);
448
449	return ret;
450}
451
452static void imx8qxp_pxl2dpi_bridge_remove(struct platform_device *pdev)
453{
454	struct imx8qxp_pxl2dpi *p2d = platform_get_drvdata(pdev);
455
456	drm_bridge_remove(&p2d->bridge);
457
458	pm_runtime_disable(&pdev->dev);
459}
460
461static const struct of_device_id imx8qxp_pxl2dpi_dt_ids[] = {
462	{ .compatible = "fsl,imx8qxp-pxl2dpi", },
463	{ /* sentinel */ }
464};
465MODULE_DEVICE_TABLE(of, imx8qxp_pxl2dpi_dt_ids);
466
467static struct platform_driver imx8qxp_pxl2dpi_bridge_driver = {
468	.probe	= imx8qxp_pxl2dpi_bridge_probe,
469	.remove = imx8qxp_pxl2dpi_bridge_remove,
470	.driver	= {
471		.of_match_table = imx8qxp_pxl2dpi_dt_ids,
472		.name = DRIVER_NAME,
473	},
474};
475module_platform_driver(imx8qxp_pxl2dpi_bridge_driver);
476
477MODULE_DESCRIPTION("i.MX8QXP pixel link to DPI bridge driver");
478MODULE_AUTHOR("Liu Ying <victor.liu@nxp.com>");
479MODULE_LICENSE("GPL v2");
480MODULE_ALIAS("platform:" DRIVER_NAME);
~/ Read-only file preview.