1 // SPDX-License-Identifier: GPL-2.0-only 2 #include <linux/component.h> 3 #include <linux/export.h> 4 #include <linux/list.h> 5 #include <linux/media-bus-format.h> 6 #include <linux/of.h> 7 #include <linux/of_graph.h> 8 9 #include <drm/drm_bridge.h> 10 #include <drm/drm_crtc.h> 11 #include <drm/drm_device.h> 12 #include <drm/drm_encoder.h> 13 #include <drm/drm_mipi_dsi.h> 14 #include <drm/drm_of.h> 15 #include <drm/drm_panel.h> 16 17 /** 18 * DOC: overview 19 * 20 * A set of helper functions to aid DRM drivers in parsing standard DT 21 * properties. 22 */ 23 24 /** 25 * drm_of_crtc_port_mask - find the mask of a registered CRTC by port OF node 26 * @dev: DRM device 27 * @port: port OF node 28 * 29 * Given a port OF node, return the possible mask of the corresponding 30 * CRTC within a device's list of CRTCs. Returns zero if not found. 31 */ 32 uint32_t drm_of_crtc_port_mask(struct drm_device *dev, 33 struct device_node *port) 34 { 35 unsigned int index = 0; 36 struct drm_crtc *tmp; 37 38 drm_for_each_crtc(tmp, dev) { 39 if (tmp->port == port) 40 return 1 << index; 41 42 index++; 43 } 44 45 return 0; 46 } 47 EXPORT_SYMBOL(drm_of_crtc_port_mask); 48 49 /** 50 * drm_of_find_possible_crtcs - find the possible CRTCs for an encoder port 51 * @dev: DRM device 52 * @port: encoder port to scan for endpoints 53 * 54 * Scan all endpoints attached to a port, locate their attached CRTCs, 55 * and generate the DRM mask of CRTCs which may be attached to this 56 * encoder. 57 * 58 * See https://github.com/devicetree-org/dt-schema/blob/main/dtschema/schemas/graph.yaml 59 * for the bindings. 60 */ 61 uint32_t drm_of_find_possible_crtcs(struct drm_device *dev, 62 struct device_node *port) 63 { 64 struct device_node *remote_port, *ep; 65 uint32_t possible_crtcs = 0; 66 67 for_each_endpoint_of_node(port, ep) { 68 remote_port = of_graph_get_remote_port(ep); 69 if (!remote_port) { 70 of_node_put(ep); 71 return 0; 72 } 73 74 possible_crtcs |= drm_of_crtc_port_mask(dev, remote_port); 75 76 of_node_put(remote_port); 77 } 78 79 return possible_crtcs; 80 } 81 EXPORT_SYMBOL(drm_of_find_possible_crtcs); 82 83 /** 84 * drm_of_component_match_add - Add a component helper OF node match rule 85 * @master: master device 86 * @matchptr: component match pointer 87 * @compare: compare function used for matching component 88 * @node: of_node 89 */ 90 void drm_of_component_match_add(struct device *master, 91 struct component_match **matchptr, 92 int (*compare)(struct device *, void *), 93 struct device_node *node) 94 { 95 of_node_get(node); 96 component_match_add_release(master, matchptr, component_release_of, 97 compare, node); 98 } 99 EXPORT_SYMBOL_GPL(drm_of_component_match_add); 100 101 /** 102 * drm_of_component_probe - Generic probe function for a component based master 103 * @dev: master device containing the OF node 104 * @compare_of: compare function used for matching components 105 * @m_ops: component master ops to be used 106 * 107 * Parse the platform device OF node and bind all the components associated 108 * with the master. Interface ports are added before the encoders in order to 109 * satisfy their .bind requirements 110 * 111 * See https://github.com/devicetree-org/dt-schema/blob/main/dtschema/schemas/graph.yaml 112 * for the bindings. 113 * 114 * Returns zero if successful, or one of the standard error codes if it fails. 115 */ 116 int drm_of_component_probe(struct device *dev, 117 int (*compare_of)(struct device *, void *), 118 const struct component_master_ops *m_ops) 119 { 120 struct device_node *ep, *port, *remote; 121 struct component_match *match = NULL; 122 int i; 123 124 if (!dev->of_node) 125 return -EINVAL; 126 127 /* 128 * Bind the crtc's ports first, so that drm_of_find_possible_crtcs() 129 * called from encoder's .bind callbacks works as expected 130 */ 131 for (i = 0; ; i++) { 132 port = of_parse_phandle(dev->of_node, "ports", i); 133 if (!port) 134 break; 135 136 if (of_device_is_available(port->parent)) 137 drm_of_component_match_add(dev, &match, compare_of, 138 port); 139 140 of_node_put(port); 141 } 142 143 if (i == 0) { 144 dev_err(dev, "missing 'ports' property\n"); 145 return -ENODEV; 146 } 147 148 if (!match) { 149 dev_err(dev, "no available port\n"); 150 return -ENODEV; 151 } 152 153 /* 154 * For bound crtcs, bind the encoders attached to their remote endpoint 155 */ 156 for (i = 0; ; i++) { 157 port = of_parse_phandle(dev->of_node, "ports", i); 158 if (!port) 159 break; 160 161 if (!of_device_is_available(port->parent)) { 162 of_node_put(port); 163 continue; 164 } 165 166 for_each_child_of_node(port, ep) { 167 remote = of_graph_get_remote_port_parent(ep); 168 if (!remote || !of_device_is_available(remote)) { 169 of_node_put(remote); 170 continue; 171 } else if (!of_device_is_available(remote->parent)) { 172 dev_warn(dev, "parent device of %pOF is not available\n", 173 remote); 174 of_node_put(remote); 175 continue; 176 } 177 178 drm_of_component_match_add(dev, &match, compare_of, 179 remote); 180 of_node_put(remote); 181 } 182 of_node_put(port); 183 } 184 185 return component_master_add_with_match(dev, m_ops, match); 186 } 187 EXPORT_SYMBOL(drm_of_component_probe); 188 189 /* 190 * drm_of_encoder_active_endpoint - return the active encoder endpoint 191 * @node: device tree node containing encoder input ports 192 * @encoder: drm_encoder 193 * 194 * Given an encoder device node and a drm_encoder with a connected crtc, 195 * parse the encoder endpoint connecting to the crtc port. 196 */ 197 int drm_of_encoder_active_endpoint(struct device_node *node, 198 struct drm_encoder *encoder, 199 struct of_endpoint *endpoint) 200 { 201 struct device_node *ep; 202 struct drm_crtc *crtc = encoder->crtc; 203 struct device_node *port; 204 int ret; 205 206 if (!node || !crtc) 207 return -EINVAL; 208 209 for_each_endpoint_of_node(node, ep) { 210 port = of_graph_get_remote_port(ep); 211 of_node_put(port); 212 if (port == crtc->port) { 213 ret = of_graph_parse_endpoint(ep, endpoint); 214 of_node_put(ep); 215 return ret; 216 } 217 } 218 219 return -EINVAL; 220 } 221 EXPORT_SYMBOL_GPL(drm_of_encoder_active_endpoint); 222 223 /** 224 * drm_of_find_panel_or_bridge - return connected panel or bridge device 225 * @np: device tree node containing encoder output ports 226 * @port: port in the device tree node 227 * @endpoint: endpoint in the device tree node 228 * @panel: pointer to hold returned drm_panel, must not be NULL 229 * @bridge: pointer to hold returned drm_bridge 230 * 231 * Given a DT node's port and endpoint number, find the connected node and 232 * return either the associated struct drm_panel or drm_bridge device. 233 * 234 * This function is deprecated and should not be used in new drivers. Use 235 * of_drm_get_bridge_by_endpoint() instead when not looking for a panel, or 236 * devm_drm_of_get_bridge() otherwise. 237 * 238 * Returns zero if successful, or one of the standard error codes if it fails. 239 */ 240 int drm_of_find_panel_or_bridge(const struct device_node *np, 241 int port, int endpoint, 242 struct drm_panel **panel, 243 struct drm_bridge **bridge) 244 { 245 int ret = -EPROBE_DEFER; 246 struct device_node *remote; 247 248 if (WARN_ON(!panel)) 249 return -EINVAL; 250 251 *panel = NULL; 252 253 /* 254 * of_graph_get_remote_node() produces a noisy error message if port 255 * node isn't found and the absence of the port is a legit case here, 256 * so at first we silently check whether graph presents in the 257 * device-tree node. 258 */ 259 if (!of_graph_is_present(np)) 260 return -ENODEV; 261 262 remote = of_graph_get_remote_node(np, port, endpoint); 263 if (!remote) 264 return -ENODEV; 265 266 *panel = of_drm_find_panel(remote); 267 if (!IS_ERR(*panel)) 268 ret = 0; 269 else 270 *panel = NULL; 271 272 if (bridge) { 273 if (ret) { 274 /* No panel found yet, check for a bridge next. */ 275 *bridge = of_drm_find_bridge(remote); 276 if (*bridge) 277 ret = 0; 278 } else { 279 *bridge = NULL; 280 } 281 282 } 283 284 of_node_put(remote); 285 return ret; 286 } 287 EXPORT_SYMBOL_GPL(drm_of_find_panel_or_bridge); 288 289 enum drm_of_lvds_pixels { 290 DRM_OF_LVDS_EVEN = BIT(0), 291 DRM_OF_LVDS_ODD = BIT(1), 292 }; 293 294 static int drm_of_lvds_get_port_pixels_type(struct device_node *port_node) 295 { 296 bool even_pixels = 297 of_property_read_bool(port_node, "dual-lvds-even-pixels"); 298 bool odd_pixels = 299 of_property_read_bool(port_node, "dual-lvds-odd-pixels"); 300 301 return (even_pixels ? DRM_OF_LVDS_EVEN : 0) | 302 (odd_pixels ? DRM_OF_LVDS_ODD : 0); 303 } 304 305 static int drm_of_lvds_get_remote_pixels_type( 306 const struct device_node *port_node) 307 { 308 struct device_node *endpoint = NULL; 309 int pixels_type = -EPIPE; 310 311 for_each_child_of_node(port_node, endpoint) { 312 struct device_node *remote_port; 313 int current_pt; 314 315 if (!of_node_name_eq(endpoint, "endpoint")) 316 continue; 317 318 remote_port = of_graph_get_remote_port(endpoint); 319 if (!remote_port) { 320 of_node_put(endpoint); 321 return -EPIPE; 322 } 323 324 current_pt = drm_of_lvds_get_port_pixels_type(remote_port); 325 of_node_put(remote_port); 326 if (pixels_type < 0) 327 pixels_type = current_pt; 328 329 /* 330 * Sanity check, ensure that all remote endpoints have the same 331 * pixel type. We may lift this restriction later if we need to 332 * support multiple sinks with different dual-link 333 * configurations by passing the endpoints explicitly to 334 * drm_of_lvds_get_dual_link_pixel_order(). 335 */ 336 if (!current_pt || pixels_type != current_pt) { 337 of_node_put(endpoint); 338 return -EINVAL; 339 } 340 } 341 342 return pixels_type; 343 } 344 345 static int __drm_of_lvds_get_dual_link_pixel_order(int p1_pt, int p2_pt) 346 { 347 /* 348 * A valid dual-lVDS bus is found when one port is marked with 349 * "dual-lvds-even-pixels", and the other port is marked with 350 * "dual-lvds-odd-pixels", bail out if the markers are not right. 351 */ 352 if (p1_pt + p2_pt != DRM_OF_LVDS_EVEN + DRM_OF_LVDS_ODD) 353 return -EINVAL; 354 355 return p1_pt == DRM_OF_LVDS_EVEN ? 356 DRM_LVDS_DUAL_LINK_EVEN_ODD_PIXELS : 357 DRM_LVDS_DUAL_LINK_ODD_EVEN_PIXELS; 358 } 359 360 /** 361 * drm_of_lvds_get_dual_link_pixel_order - Get LVDS dual-link source pixel order 362 * @port1: First DT port node of the Dual-link LVDS source 363 * @port2: Second DT port node of the Dual-link LVDS source 364 * 365 * An LVDS dual-link connection is made of two links, with even pixels 366 * transitting on one link, and odd pixels on the other link. This function 367 * returns, for two ports of an LVDS dual-link source, which port shall transmit 368 * the even and odd pixels, based on the requirements of the connected sink. 369 * 370 * The pixel order is determined from the dual-lvds-even-pixels and 371 * dual-lvds-odd-pixels properties in the sink's DT port nodes. If those 372 * properties are not present, or if their usage is not valid, this function 373 * returns -EINVAL. 374 * 375 * If either port is not connected, this function returns -EPIPE. 376 * 377 * @port1 and @port2 are typically DT sibling nodes, but may have different 378 * parents when, for instance, two separate LVDS encoders carry the even and odd 379 * pixels. 380 * 381 * Return: 382 * * DRM_LVDS_DUAL_LINK_EVEN_ODD_PIXELS - @port1 carries even pixels and @port2 383 * carries odd pixels 384 * * DRM_LVDS_DUAL_LINK_ODD_EVEN_PIXELS - @port1 carries odd pixels and @port2 385 * carries even pixels 386 * * -EINVAL - @port1 and @port2 are not connected to a dual-link LVDS sink, or 387 * the sink configuration is invalid 388 * * -EPIPE - when @port1 or @port2 are not connected 389 */ 390 int drm_of_lvds_get_dual_link_pixel_order(const struct device_node *port1, 391 const struct device_node *port2) 392 { 393 int remote_p1_pt, remote_p2_pt; 394 395 if (!port1 || !port2) 396 return -EINVAL; 397 398 remote_p1_pt = drm_of_lvds_get_remote_pixels_type(port1); 399 if (remote_p1_pt < 0) 400 return remote_p1_pt; 401 402 remote_p2_pt = drm_of_lvds_get_remote_pixels_type(port2); 403 if (remote_p2_pt < 0) 404 return remote_p2_pt; 405 406 return __drm_of_lvds_get_dual_link_pixel_order(remote_p1_pt, remote_p2_pt); 407 } 408 EXPORT_SYMBOL_GPL(drm_of_lvds_get_dual_link_pixel_order); 409 410 /** 411 * drm_of_lvds_get_dual_link_pixel_order_sink - Get LVDS dual-link sink pixel order 412 * @port1: First DT port node of the Dual-link LVDS sink 413 * @port2: Second DT port node of the Dual-link LVDS sink 414 * 415 * An LVDS dual-link connection is made of two links, with even pixels 416 * transitting on one link, and odd pixels on the other link. This function 417 * returns, for two ports of an LVDS dual-link sink, which port shall transmit 418 * the even and odd pixels, based on the requirements of the sink. 419 * 420 * The pixel order is determined from the dual-lvds-even-pixels and 421 * dual-lvds-odd-pixels properties in the sink's DT port nodes. If those 422 * properties are not present, or if their usage is not valid, this function 423 * returns -EINVAL. 424 * 425 * If either port is not connected, this function returns -EPIPE. 426 * 427 * @port1 and @port2 are typically DT sibling nodes, but may have different 428 * parents when, for instance, two separate LVDS decoders receive the even and 429 * odd pixels. 430 * 431 * Return: 432 * * DRM_LVDS_DUAL_LINK_EVEN_ODD_PIXELS - @port1 receives even pixels and @port2 433 * receives odd pixels 434 * * DRM_LVDS_DUAL_LINK_ODD_EVEN_PIXELS - @port1 receives odd pixels and @port2 435 * receives even pixels 436 * * -EINVAL - @port1 or @port2 are NULL 437 * * -EPIPE - when @port1 or @port2 are not connected 438 */ 439 int drm_of_lvds_get_dual_link_pixel_order_sink(struct device_node *port1, 440 struct device_node *port2) 441 { 442 int sink_p1_pt, sink_p2_pt; 443 444 if (!port1 || !port2) 445 return -EINVAL; 446 447 sink_p1_pt = drm_of_lvds_get_port_pixels_type(port1); 448 if (!sink_p1_pt) 449 return -EPIPE; 450 451 sink_p2_pt = drm_of_lvds_get_port_pixels_type(port2); 452 if (!sink_p2_pt) 453 return -EPIPE; 454 455 return __drm_of_lvds_get_dual_link_pixel_order(sink_p1_pt, sink_p2_pt); 456 } 457 EXPORT_SYMBOL_GPL(drm_of_lvds_get_dual_link_pixel_order_sink); 458 459 /** 460 * drm_of_lvds_get_data_mapping - Get LVDS data mapping 461 * @port: DT port node of the LVDS source or sink 462 * 463 * Convert DT "data-mapping" property string value into media bus format value. 464 * 465 * Return: 466 * * MEDIA_BUS_FMT_RGB666_1X7X3_SPWG - data-mapping is "jeida-18" 467 * * MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA - data-mapping is "jeida-24" 468 * * MEDIA_BUS_FMT_RGB101010_1X7X5_JEIDA - data-mapping is "jeida-30" 469 * * MEDIA_BUS_FMT_RGB888_1X7X4_SPWG - data-mapping is "vesa-24" 470 * * MEDIA_BUS_FMT_RGB101010_1X7X5_SPWG - data-mapping is "vesa-30" 471 * * -EINVAL - the "data-mapping" property is unsupported 472 * * -ENODEV - the "data-mapping" property is missing 473 */ 474 int drm_of_lvds_get_data_mapping(const struct device_node *port) 475 { 476 const char *mapping; 477 int ret; 478 479 ret = of_property_read_string(port, "data-mapping", &mapping); 480 if (ret < 0) 481 return -ENODEV; 482 483 if (!strcmp(mapping, "jeida-18")) 484 return MEDIA_BUS_FMT_RGB666_1X7X3_SPWG; 485 if (!strcmp(mapping, "jeida-24")) 486 return MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA; 487 if (!strcmp(mapping, "jeida-30")) 488 return MEDIA_BUS_FMT_RGB101010_1X7X5_JEIDA; 489 if (!strcmp(mapping, "vesa-24")) 490 return MEDIA_BUS_FMT_RGB888_1X7X4_SPWG; 491 if (!strcmp(mapping, "vesa-30")) 492 return MEDIA_BUS_FMT_RGB101010_1X7X5_SPWG; 493 494 return -EINVAL; 495 } 496 EXPORT_SYMBOL_GPL(drm_of_lvds_get_data_mapping); 497 498 /** 499 * drm_of_get_data_lanes_count - Get DSI/(e)DP data lane count 500 * @endpoint: DT endpoint node of the DSI/(e)DP source or sink 501 * @min: minimum supported number of data lanes 502 * @max: maximum supported number of data lanes 503 * 504 * Count DT "data-lanes" property elements and check for validity. 505 * 506 * Return: 507 * * min..max - positive integer count of "data-lanes" elements 508 * * -ve - the "data-lanes" property is missing or invalid 509 * * -EINVAL - the "data-lanes" property is unsupported 510 */ 511 int drm_of_get_data_lanes_count(const struct device_node *endpoint, 512 const unsigned int min, const unsigned int max) 513 { 514 int ret; 515 516 ret = of_property_count_u32_elems(endpoint, "data-lanes"); 517 if (ret < 0) 518 return ret; 519 520 if (ret < min || ret > max) 521 return -EINVAL; 522 523 return ret; 524 } 525 EXPORT_SYMBOL_GPL(drm_of_get_data_lanes_count); 526 527 /** 528 * drm_of_get_data_lanes_count_ep - Get DSI/(e)DP data lane count by endpoint 529 * @port: DT port node of the DSI/(e)DP source or sink 530 * @port_reg: identifier (value of reg property) of the parent port node 531 * @reg: identifier (value of reg property) of the endpoint node 532 * @min: minimum supported number of data lanes 533 * @max: maximum supported number of data lanes 534 * 535 * Count DT "data-lanes" property elements and check for validity. 536 * This variant uses endpoint specifier. 537 * 538 * Return: 539 * * min..max - positive integer count of "data-lanes" elements 540 * * -EINVAL - the "data-mapping" property is unsupported 541 * * -ENODEV - the "data-mapping" property is missing 542 */ 543 int drm_of_get_data_lanes_count_ep(const struct device_node *port, 544 int port_reg, int reg, 545 const unsigned int min, 546 const unsigned int max) 547 { 548 struct device_node *endpoint; 549 int ret; 550 551 endpoint = of_graph_get_endpoint_by_regs(port, port_reg, reg); 552 ret = drm_of_get_data_lanes_count(endpoint, min, max); 553 of_node_put(endpoint); 554 555 return ret; 556 } 557 EXPORT_SYMBOL_GPL(drm_of_get_data_lanes_count_ep); 558 559 /** 560 * drm_of_get_data_lanes_count_remote - Get DSI/(e)DP data lane count by endpoint 561 * @port: DT port node of the DSI/(e)DP source or sink 562 * @port_reg: identifier (value of reg property) of the parent port node 563 * @reg: identifier (value of reg property) of the endpoint node 564 * @min: minimum supported number of data lanes 565 * @max: maximum supported number of data lanes 566 * 567 * Count DT "data-lanes" property elements in the remote endpoint and check for 568 * validity. This variant uses endpoint specifier. 569 * 570 * Return: 571 * * min..max - positive integer count of "data-lanes" elements 572 * * -EINVAL - the "data-lanes" property is unsupported 573 * * -ENODEV - the "data-lanes" property is missing 574 */ 575 int drm_of_get_data_lanes_count_remote(const struct device_node *port, 576 int port_reg, int reg, 577 const unsigned int min, 578 const unsigned int max) 579 { 580 struct device_node *endpoint, *remote; 581 int ret; 582 583 endpoint = of_graph_get_endpoint_by_regs(port, port_reg, reg); 584 remote = of_graph_get_remote_endpoint(endpoint); 585 of_node_put(endpoint); 586 ret = drm_of_get_data_lanes_count(remote, min, max); 587 of_node_put(remote); 588 589 return ret; 590 } 591 EXPORT_SYMBOL_GPL(drm_of_get_data_lanes_count_remote); 592 593 #if IS_ENABLED(CONFIG_DRM_MIPI_DSI) 594 595 /** 596 * drm_of_get_dsi_bus - find the DSI bus for a given device 597 * @dev: parent device of display (SPI, I2C) 598 * 599 * Gets parent DSI bus for a DSI device controlled through a bus other 600 * than MIPI-DCS (SPI, I2C, etc.) using the Device Tree. 601 * 602 * This function assumes that the device's port@0 is the DSI input. 603 * 604 * Returns pointer to mipi_dsi_host if successful, -EINVAL if the 605 * request is unsupported, -EPROBE_DEFER if the DSI host is found but 606 * not available, or -ENODEV otherwise. 607 */ 608 struct mipi_dsi_host *drm_of_get_dsi_bus(struct device *dev) 609 { 610 struct mipi_dsi_host *dsi_host; 611 struct device_node *endpoint, *dsi_host_node; 612 613 /* 614 * Get first endpoint child from device. 615 */ 616 endpoint = of_graph_get_endpoint_by_regs(dev->of_node, 0, -1); 617 if (!endpoint) 618 return ERR_PTR(-ENODEV); 619 620 /* 621 * Follow the first endpoint to get the DSI host node and then 622 * release the endpoint since we no longer need it. 623 */ 624 dsi_host_node = of_graph_get_remote_port_parent(endpoint); 625 of_node_put(endpoint); 626 if (!dsi_host_node) 627 return ERR_PTR(-ENODEV); 628 629 /* 630 * Get the DSI host from the DSI host node. If we get an error 631 * or the return is null assume we're not ready to probe just 632 * yet. Release the DSI host node since we're done with it. 633 */ 634 dsi_host = of_find_mipi_dsi_host_by_node(dsi_host_node); 635 of_node_put(dsi_host_node); 636 if (IS_ERR_OR_NULL(dsi_host)) 637 return ERR_PTR(-EPROBE_DEFER); 638 639 return dsi_host; 640 } 641 EXPORT_SYMBOL_GPL(drm_of_get_dsi_bus); 642 643 #endif /* CONFIG_DRM_MIPI_DSI */ 644