1 // SPDX-License-Identifier: GPL-2.0+
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>
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>
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>
23 #include <dt-bindings/firmware/imx/rsrc.h>
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
33 #define DRIVER_NAME "imx8qxp-pxl2dpi"
35 struct imx8qxp_pxl2dpi {
36 struct regmap *regmap;
37 struct drm_bridge bridge;
38 struct drm_bridge *next_bridge;
39 struct drm_bridge *companion;
41 struct imx_sc_ipc *ipc_handle;
48 #define bridge_to_p2d(b) container_of(b, struct imx8qxp_pxl2dpi, bridge)
50 static int imx8qxp_pxl2dpi_bridge_attach(struct drm_bridge *bridge,
51 enum drm_bridge_attach_flags flags)
53 struct imx8qxp_pxl2dpi *p2d = bridge->driver_private;
55 if (!(flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)) {
56 DRM_DEV_ERROR(p2d->dev,
57 "do not support creating a drm_connector\n");
61 return drm_bridge_attach(bridge->encoder,
62 p2d->next_bridge, bridge,
63 DRM_BRIDGE_ATTACH_NO_CONNECTOR);
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)
72 struct imx8qxp_pxl2dpi *p2d = bridge->driver_private;
74 p2d->in_bus_format = bridge_state->input_bus_cfg.format;
75 p2d->out_bus_format = bridge_state->output_bus_cfg.format;
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)
85 struct imx8qxp_pxl2dpi *p2d = bridge->driver_private;
86 struct imx8qxp_pxl2dpi *companion_p2d;
89 ret = pm_runtime_get_sync(p2d->dev);
91 DRM_DEV_ERROR(p2d->dev,
92 "failed to get runtime PM sync: %d\n", ret);
94 ret = imx_sc_misc_set_control(p2d->ipc_handle, p2d->sc_resource,
95 IMX_SC_C_PXL_LINK_SEL, p2d->pl_sel);
97 DRM_DEV_ERROR(p2d->dev,
98 "failed to set pixel link selection(%u): %d\n",
101 switch (p2d->out_bus_format) {
102 case MEDIA_BUS_FMT_RGB888_1X24:
103 regmap_write(p2d->regmap, PXL2DPI_CTRL, CFG_24BIT);
105 case MEDIA_BUS_FMT_RGB666_1X24_CPADHI:
106 regmap_write(p2d->regmap, PXL2DPI_CTRL, CFG2_18BIT);
109 DRM_DEV_ERROR(p2d->dev,
110 "unsupported output bus format 0x%08x\n",
111 p2d->out_bus_format);
114 if (p2d->companion) {
115 companion_p2d = bridge_to_p2d(p2d->companion);
117 companion_p2d->in_bus_format = p2d->in_bus_format;
118 companion_p2d->out_bus_format = p2d->out_bus_format;
120 p2d->companion->funcs->mode_set(p2d->companion, mode,
126 imx8qxp_pxl2dpi_bridge_atomic_disable(struct drm_bridge *bridge,
127 struct drm_bridge_state *old_bridge_state)
129 struct imx8qxp_pxl2dpi *p2d = bridge->driver_private;
132 ret = pm_runtime_put(p2d->dev);
134 DRM_DEV_ERROR(p2d->dev, "failed to put runtime PM: %d\n", ret);
137 p2d->companion->funcs->atomic_disable(p2d->companion,
141 static const u32 imx8qxp_pxl2dpi_bus_output_fmts[] = {
142 MEDIA_BUS_FMT_RGB888_1X24,
143 MEDIA_BUS_FMT_RGB666_1X24_CPADHI,
146 static bool imx8qxp_pxl2dpi_bus_output_fmt_supported(u32 fmt)
150 for (i = 0; i < ARRAY_SIZE(imx8qxp_pxl2dpi_bus_output_fmts); i++) {
151 if (imx8qxp_pxl2dpi_bus_output_fmts[i] == fmt)
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,
164 unsigned int *num_input_fmts)
168 if (!imx8qxp_pxl2dpi_bus_output_fmt_supported(output_fmt))
173 input_fmts = kmalloc(sizeof(*input_fmts), GFP_KERNEL);
177 switch (output_fmt) {
178 case MEDIA_BUS_FMT_RGB888_1X24:
179 input_fmts[0] = MEDIA_BUS_FMT_RGB888_1X36_CPADLO;
181 case MEDIA_BUS_FMT_RGB666_1X24_CPADHI:
182 input_fmts[0] = MEDIA_BUS_FMT_RGB666_1X36_CPADLO;
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)
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);
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,
219 static struct device_node *
220 imx8qxp_pxl2dpi_get_available_ep_from_port(struct imx8qxp_pxl2dpi *p2d,
223 struct device_node *port, *ep;
226 port = of_graph_get_port_by_id(p2d->dev->of_node, port_id);
228 DRM_DEV_ERROR(p2d->dev, "failed to get port@%u\n", port_id);
229 return ERR_PTR(-ENODEV);
232 ep_cnt = of_get_available_child_count(port);
234 DRM_DEV_ERROR(p2d->dev, "no available endpoints of port@%u\n",
236 ep = ERR_PTR(-ENODEV);
238 } else if (ep_cnt > 1) {
239 DRM_DEV_ERROR(p2d->dev,
240 "invalid available endpoints of port@%u\n",
242 ep = ERR_PTR(-EINVAL);
246 ep = of_get_next_available_child(port, NULL);
248 DRM_DEV_ERROR(p2d->dev,
249 "failed to get available endpoint of port@%u\n",
251 ep = ERR_PTR(-ENODEV);
259 static struct drm_bridge *
260 imx8qxp_pxl2dpi_find_next_bridge(struct imx8qxp_pxl2dpi *p2d)
262 struct device_node *ep, *remote;
263 struct drm_bridge *next_bridge;
266 ep = imx8qxp_pxl2dpi_get_available_ep_from_port(p2d, 1);
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);
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);
283 next_bridge = of_drm_find_bridge(remote);
285 next_bridge = ERR_PTR(-EPROBE_DEFER);
295 static int imx8qxp_pxl2dpi_set_pixel_link_sel(struct imx8qxp_pxl2dpi *p2d)
297 struct device_node *ep;
298 struct of_endpoint endpoint;
301 ep = imx8qxp_pxl2dpi_get_available_ep_from_port(p2d, 0);
305 ret = of_graph_parse_endpoint(ep, &endpoint);
307 DRM_DEV_ERROR(p2d->dev,
308 "failed to parse endpoint of port@0: %d\n", ret);
312 p2d->pl_sel = endpoint.id;
319 static int imx8qxp_pxl2dpi_parse_dt_companion(struct imx8qxp_pxl2dpi *p2d)
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;
329 /* Locate the companion PXL2DPI for dual-link operation, if any. */
330 companion = of_parse_phandle(dev->of_node, "fsl,companion-pxl2dpi", 0);
334 if (!of_device_is_available(companion)) {
335 DRM_DEV_ERROR(dev, "companion PXL2DPI is not available\n");
341 * Sanity check: the companion bridge must have the same compatible
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");
351 p2d->companion = of_drm_find_bridge(companion);
352 if (!p2d->companion) {
354 DRM_DEV_DEBUG_DRIVER(p2d->dev,
355 "failed to find companion bridge: %d\n",
360 companion_p2d = bridge_to_p2d(p2d->companion);
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.
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);
376 DRM_DEV_ERROR(dev, "failed to get dual link pixel order: %d\n",
381 DRM_DEV_DEBUG_DRIVER(dev,
382 "dual-link configuration detected (companion bridge %pOF)\n",
385 of_node_put(companion);
389 static int imx8qxp_pxl2dpi_bridge_probe(struct platform_device *pdev)
391 struct imx8qxp_pxl2dpi *p2d;
392 struct device *dev = &pdev->dev;
393 struct device_node *np = dev->of_node;
396 p2d = devm_kzalloc(dev, sizeof(*p2d), GFP_KERNEL);
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);
408 ret = imx_scu_get_handle(&p2d->ipc_handle);
410 if (ret != -EPROBE_DEFER)
411 DRM_DEV_ERROR(dev, "failed to get SCU ipc handle: %d\n",
418 ret = of_property_read_u32(np, "fsl,sc-resource", &p2d->sc_resource);
420 DRM_DEV_ERROR(dev, "failed to get SC resource %d\n", ret);
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",
433 ret = imx8qxp_pxl2dpi_set_pixel_link_sel(p2d);
437 ret = imx8qxp_pxl2dpi_parse_dt_companion(p2d);
441 platform_set_drvdata(pdev, p2d);
442 pm_runtime_enable(dev);
444 p2d->bridge.driver_private = p2d;
445 p2d->bridge.funcs = &imx8qxp_pxl2dpi_bridge_funcs;
446 p2d->bridge.of_node = np;
448 drm_bridge_add(&p2d->bridge);
453 static void imx8qxp_pxl2dpi_bridge_remove(struct platform_device *pdev)
455 struct imx8qxp_pxl2dpi *p2d = platform_get_drvdata(pdev);
457 drm_bridge_remove(&p2d->bridge);
459 pm_runtime_disable(&pdev->dev);
462 static const struct of_device_id imx8qxp_pxl2dpi_dt_ids[] = {
463 { .compatible = "fsl,imx8qxp-pxl2dpi", },
466 MODULE_DEVICE_TABLE(of, imx8qxp_pxl2dpi_dt_ids);
468 static struct platform_driver imx8qxp_pxl2dpi_bridge_driver = {
469 .probe = imx8qxp_pxl2dpi_bridge_probe,
470 .remove = imx8qxp_pxl2dpi_bridge_remove,
472 .of_match_table = imx8qxp_pxl2dpi_dt_ids,
476 module_platform_driver(imx8qxp_pxl2dpi_bridge_driver);
478 MODULE_DESCRIPTION("i.MX8QXP pixel link to DPI bridge driver");
480 MODULE_LICENSE("GPL v2");
481 MODULE_ALIAS("platform:" DRIVER_NAME);