1 // SPDX-License-Identifier: GPL-2.0+
4 * Copyright 2020,2022 NXP
7 #include <linux/firmware/imx/svc/misc.h>
8 #include <linux/media-bus-format.h>
9 #include <linux/module.h>
11 #include <linux/of_graph.h>
12 #include <linux/platform_device.h>
14 #include <drm/drm_atomic_state_helper.h>
15 #include <drm/drm_bridge.h>
16 #include <drm/drm_print.h>
18 #include <dt-bindings/firmware/imx/rsrc.h>
20 #define DRIVER_NAME "imx8qxp-display-pixel-link"
21 #define PL_MAX_MST_ADDR 3
22 #define PL_MAX_NEXT_BRIDGES 2
24 struct imx8qxp_pixel_link
{
25 struct drm_bridge bridge
;
26 struct drm_bridge
*next_bridge
;
28 struct imx_sc_ipc
*ipc_handle
;
39 static void imx8qxp_pixel_link_enable_mst_en(struct imx8qxp_pixel_link
*pl
)
43 ret
= imx_sc_misc_set_control(pl
->ipc_handle
, pl
->sink_rsc
,
44 pl
->mst_en_ctrl
, true);
46 DRM_DEV_ERROR(pl
->dev
,
47 "failed to enable DC%u stream%u pixel link mst_en: %d\n",
48 pl
->dc_id
, pl
->stream_id
, ret
);
51 static void imx8qxp_pixel_link_enable_mst_vld(struct imx8qxp_pixel_link
*pl
)
55 ret
= imx_sc_misc_set_control(pl
->ipc_handle
, pl
->sink_rsc
,
56 pl
->mst_vld_ctrl
, true);
58 DRM_DEV_ERROR(pl
->dev
,
59 "failed to enable DC%u stream%u pixel link mst_vld: %d\n",
60 pl
->dc_id
, pl
->stream_id
, ret
);
63 static void imx8qxp_pixel_link_enable_sync(struct imx8qxp_pixel_link
*pl
)
67 ret
= imx_sc_misc_set_control(pl
->ipc_handle
, pl
->sink_rsc
,
70 DRM_DEV_ERROR(pl
->dev
,
71 "failed to enable DC%u stream%u pixel link sync: %d\n",
72 pl
->dc_id
, pl
->stream_id
, ret
);
75 static int imx8qxp_pixel_link_disable_mst_en(struct imx8qxp_pixel_link
*pl
)
79 ret
= imx_sc_misc_set_control(pl
->ipc_handle
, pl
->sink_rsc
,
80 pl
->mst_en_ctrl
, false);
82 DRM_DEV_ERROR(pl
->dev
,
83 "failed to disable DC%u stream%u pixel link mst_en: %d\n",
84 pl
->dc_id
, pl
->stream_id
, ret
);
89 static int imx8qxp_pixel_link_disable_mst_vld(struct imx8qxp_pixel_link
*pl
)
93 ret
= imx_sc_misc_set_control(pl
->ipc_handle
, pl
->sink_rsc
,
94 pl
->mst_vld_ctrl
, false);
96 DRM_DEV_ERROR(pl
->dev
,
97 "failed to disable DC%u stream%u pixel link mst_vld: %d\n",
98 pl
->dc_id
, pl
->stream_id
, ret
);
103 static int imx8qxp_pixel_link_disable_sync(struct imx8qxp_pixel_link
*pl
)
107 ret
= imx_sc_misc_set_control(pl
->ipc_handle
, pl
->sink_rsc
,
108 pl
->sync_ctrl
, false);
110 DRM_DEV_ERROR(pl
->dev
,
111 "failed to disable DC%u stream%u pixel link sync: %d\n",
112 pl
->dc_id
, pl
->stream_id
, ret
);
117 static void imx8qxp_pixel_link_set_mst_addr(struct imx8qxp_pixel_link
*pl
)
121 ret
= imx_sc_misc_set_control(pl
->ipc_handle
,
122 pl
->sink_rsc
, pl
->mst_addr_ctrl
,
125 DRM_DEV_ERROR(pl
->dev
,
126 "failed to set DC%u stream%u pixel link mst addr(%u): %d\n",
127 pl
->dc_id
, pl
->stream_id
, pl
->mst_addr
, ret
);
130 static int imx8qxp_pixel_link_bridge_attach(struct drm_bridge
*bridge
,
131 enum drm_bridge_attach_flags flags
)
133 struct imx8qxp_pixel_link
*pl
= bridge
->driver_private
;
135 if (!(flags
& DRM_BRIDGE_ATTACH_NO_CONNECTOR
)) {
136 DRM_DEV_ERROR(pl
->dev
,
137 "do not support creating a drm_connector\n");
141 return drm_bridge_attach(bridge
->encoder
,
142 pl
->next_bridge
, bridge
,
143 DRM_BRIDGE_ATTACH_NO_CONNECTOR
);
147 imx8qxp_pixel_link_bridge_mode_set(struct drm_bridge
*bridge
,
148 const struct drm_display_mode
*mode
,
149 const struct drm_display_mode
*adjusted_mode
)
151 struct imx8qxp_pixel_link
*pl
= bridge
->driver_private
;
153 imx8qxp_pixel_link_set_mst_addr(pl
);
157 imx8qxp_pixel_link_bridge_atomic_enable(struct drm_bridge
*bridge
,
158 struct drm_bridge_state
*old_bridge_state
)
160 struct imx8qxp_pixel_link
*pl
= bridge
->driver_private
;
162 imx8qxp_pixel_link_enable_mst_en(pl
);
163 imx8qxp_pixel_link_enable_mst_vld(pl
);
164 imx8qxp_pixel_link_enable_sync(pl
);
168 imx8qxp_pixel_link_bridge_atomic_disable(struct drm_bridge
*bridge
,
169 struct drm_bridge_state
*old_bridge_state
)
171 struct imx8qxp_pixel_link
*pl
= bridge
->driver_private
;
173 imx8qxp_pixel_link_disable_mst_en(pl
);
174 imx8qxp_pixel_link_disable_mst_vld(pl
);
175 imx8qxp_pixel_link_disable_sync(pl
);
178 static const u32 imx8qxp_pixel_link_bus_output_fmts
[] = {
179 MEDIA_BUS_FMT_RGB888_1X36_CPADLO
,
180 MEDIA_BUS_FMT_RGB666_1X36_CPADLO
,
183 static bool imx8qxp_pixel_link_bus_output_fmt_supported(u32 fmt
)
187 for (i
= 0; i
< ARRAY_SIZE(imx8qxp_pixel_link_bus_output_fmts
); i
++) {
188 if (imx8qxp_pixel_link_bus_output_fmts
[i
] == fmt
)
196 imx8qxp_pixel_link_bridge_atomic_get_input_bus_fmts(struct drm_bridge
*bridge
,
197 struct drm_bridge_state
*bridge_state
,
198 struct drm_crtc_state
*crtc_state
,
199 struct drm_connector_state
*conn_state
,
201 unsigned int *num_input_fmts
)
205 if (!imx8qxp_pixel_link_bus_output_fmt_supported(output_fmt
))
210 input_fmts
= kmalloc(sizeof(*input_fmts
), GFP_KERNEL
);
214 input_fmts
[0] = output_fmt
;
220 imx8qxp_pixel_link_bridge_atomic_get_output_bus_fmts(struct drm_bridge
*bridge
,
221 struct drm_bridge_state
*bridge_state
,
222 struct drm_crtc_state
*crtc_state
,
223 struct drm_connector_state
*conn_state
,
224 unsigned int *num_output_fmts
)
226 *num_output_fmts
= ARRAY_SIZE(imx8qxp_pixel_link_bus_output_fmts
);
227 return kmemdup(imx8qxp_pixel_link_bus_output_fmts
,
228 sizeof(imx8qxp_pixel_link_bus_output_fmts
), GFP_KERNEL
);
231 static const struct drm_bridge_funcs imx8qxp_pixel_link_bridge_funcs
= {
232 .atomic_duplicate_state
= drm_atomic_helper_bridge_duplicate_state
,
233 .atomic_destroy_state
= drm_atomic_helper_bridge_destroy_state
,
234 .atomic_reset
= drm_atomic_helper_bridge_reset
,
235 .attach
= imx8qxp_pixel_link_bridge_attach
,
236 .mode_set
= imx8qxp_pixel_link_bridge_mode_set
,
237 .atomic_enable
= imx8qxp_pixel_link_bridge_atomic_enable
,
238 .atomic_disable
= imx8qxp_pixel_link_bridge_atomic_disable
,
239 .atomic_get_input_bus_fmts
=
240 imx8qxp_pixel_link_bridge_atomic_get_input_bus_fmts
,
241 .atomic_get_output_bus_fmts
=
242 imx8qxp_pixel_link_bridge_atomic_get_output_bus_fmts
,
245 static int imx8qxp_pixel_link_disable_all_controls(struct imx8qxp_pixel_link
*pl
)
249 ret
= imx8qxp_pixel_link_disable_mst_en(pl
);
253 ret
= imx8qxp_pixel_link_disable_mst_vld(pl
);
257 return imx8qxp_pixel_link_disable_sync(pl
);
260 static struct drm_bridge
*
261 imx8qxp_pixel_link_find_next_bridge(struct imx8qxp_pixel_link
*pl
)
263 struct device_node
*np
= pl
->dev
->of_node
;
264 struct device_node
*port
, *remote
;
265 struct drm_bridge
*next_bridge
[PL_MAX_NEXT_BRIDGES
];
267 bool found_port
= false;
269 /* select the first next bridge by default */
272 for (port_id
= 1; port_id
<= PL_MAX_MST_ADDR
+ 1; port_id
++) {
273 port
= of_graph_get_port_by_id(np
, port_id
);
277 if (of_device_is_available(port
)) {
287 DRM_DEV_ERROR(pl
->dev
, "no available output port\n");
288 return ERR_PTR(-ENODEV
);
291 for (reg
= 0; reg
< PL_MAX_NEXT_BRIDGES
; reg
++) {
292 remote
= of_graph_get_remote_node(np
, port_id
, reg
);
296 if (!of_device_is_available(remote
->parent
)) {
297 DRM_DEV_DEBUG(pl
->dev
,
298 "port%u endpoint%u remote parent is not available\n",
304 next_bridge
[ep_cnt
] = of_drm_find_bridge(remote
);
305 if (!next_bridge
[ep_cnt
]) {
307 return ERR_PTR(-EPROBE_DEFER
);
310 /* specially select the next bridge with companion PXL2DPI */
311 if (of_property_present(remote
, "fsl,companion-pxl2dpi"))
319 pl
->mst_addr
= port_id
- 1;
321 return next_bridge
[bridge_sel
];
324 static int imx8qxp_pixel_link_bridge_probe(struct platform_device
*pdev
)
326 struct imx8qxp_pixel_link
*pl
;
327 struct device
*dev
= &pdev
->dev
;
328 struct device_node
*np
= dev
->of_node
;
331 pl
= devm_kzalloc(dev
, sizeof(*pl
), GFP_KERNEL
);
335 ret
= imx_scu_get_handle(&pl
->ipc_handle
);
337 if (ret
!= -EPROBE_DEFER
)
338 DRM_DEV_ERROR(dev
, "failed to get SCU ipc handle: %d\n",
343 ret
= of_property_read_u8(np
, "fsl,dc-id", &pl
->dc_id
);
345 DRM_DEV_ERROR(dev
, "failed to get DC index: %d\n", ret
);
349 ret
= of_property_read_u8(np
, "fsl,dc-stream-id", &pl
->stream_id
);
351 DRM_DEV_ERROR(dev
, "failed to get DC stream index: %d\n", ret
);
357 pl
->sink_rsc
= pl
->dc_id
? IMX_SC_R_DC_1
: IMX_SC_R_DC_0
;
359 if (pl
->stream_id
== 0) {
360 pl
->mst_addr_ctrl
= IMX_SC_C_PXL_LINK_MST1_ADDR
;
361 pl
->mst_en_ctrl
= IMX_SC_C_PXL_LINK_MST1_ENB
;
362 pl
->mst_vld_ctrl
= IMX_SC_C_PXL_LINK_MST1_VLD
;
363 pl
->sync_ctrl
= IMX_SC_C_SYNC_CTRL0
;
365 pl
->mst_addr_ctrl
= IMX_SC_C_PXL_LINK_MST2_ADDR
;
366 pl
->mst_en_ctrl
= IMX_SC_C_PXL_LINK_MST2_ENB
;
367 pl
->mst_vld_ctrl
= IMX_SC_C_PXL_LINK_MST2_VLD
;
368 pl
->sync_ctrl
= IMX_SC_C_SYNC_CTRL1
;
371 /* disable all controls to POR default */
372 ret
= imx8qxp_pixel_link_disable_all_controls(pl
);
376 pl
->next_bridge
= imx8qxp_pixel_link_find_next_bridge(pl
);
377 if (IS_ERR(pl
->next_bridge
)) {
378 ret
= PTR_ERR(pl
->next_bridge
);
379 if (ret
!= -EPROBE_DEFER
)
380 DRM_DEV_ERROR(dev
, "failed to find next bridge: %d\n",
385 platform_set_drvdata(pdev
, pl
);
387 pl
->bridge
.driver_private
= pl
;
388 pl
->bridge
.funcs
= &imx8qxp_pixel_link_bridge_funcs
;
389 pl
->bridge
.of_node
= np
;
391 drm_bridge_add(&pl
->bridge
);
396 static void imx8qxp_pixel_link_bridge_remove(struct platform_device
*pdev
)
398 struct imx8qxp_pixel_link
*pl
= platform_get_drvdata(pdev
);
400 drm_bridge_remove(&pl
->bridge
);
403 static const struct of_device_id imx8qxp_pixel_link_dt_ids
[] = {
404 { .compatible
= "fsl,imx8qm-dc-pixel-link", },
405 { .compatible
= "fsl,imx8qxp-dc-pixel-link", },
408 MODULE_DEVICE_TABLE(of
, imx8qxp_pixel_link_dt_ids
);
410 static struct platform_driver imx8qxp_pixel_link_bridge_driver
= {
411 .probe
= imx8qxp_pixel_link_bridge_probe
,
412 .remove
= imx8qxp_pixel_link_bridge_remove
,
414 .of_match_table
= imx8qxp_pixel_link_dt_ids
,
418 module_platform_driver(imx8qxp_pixel_link_bridge_driver
);
420 MODULE_DESCRIPTION("i.MX8QXP/QM display pixel link bridge driver");
421 MODULE_AUTHOR("Liu Ying <victor.liu@nxp.com>");
422 MODULE_LICENSE("GPL v2");
423 MODULE_ALIAS("platform:" DRIVER_NAME
);