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