Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * R-Car Display Unit Encoder
0004  *
0005  * Copyright (C) 2013-2014 Renesas Electronics Corporation
0006  *
0007  * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
0008  */
0009 
0010 #include <linux/export.h>
0011 #include <linux/of.h>
0012 #include <linux/slab.h>
0013 
0014 #include <drm/drm_bridge.h>
0015 #include <drm/drm_bridge_connector.h>
0016 #include <drm/drm_crtc.h>
0017 #include <drm/drm_managed.h>
0018 #include <drm/drm_modeset_helper_vtables.h>
0019 #include <drm/drm_panel.h>
0020 
0021 #include "rcar_du_drv.h"
0022 #include "rcar_du_encoder.h"
0023 #include "rcar_du_kms.h"
0024 #include "rcar_lvds.h"
0025 
0026 /* -----------------------------------------------------------------------------
0027  * Encoder
0028  */
0029 
0030 static unsigned int rcar_du_encoder_count_ports(struct device_node *node)
0031 {
0032     struct device_node *ports;
0033     struct device_node *port;
0034     unsigned int num_ports = 0;
0035 
0036     ports = of_get_child_by_name(node, "ports");
0037     if (!ports)
0038         ports = of_node_get(node);
0039 
0040     for_each_child_of_node(ports, port) {
0041         if (of_node_name_eq(port, "port"))
0042             num_ports++;
0043     }
0044 
0045     of_node_put(ports);
0046 
0047     return num_ports;
0048 }
0049 
0050 static const struct drm_encoder_funcs rcar_du_encoder_funcs = {
0051 };
0052 
0053 int rcar_du_encoder_init(struct rcar_du_device *rcdu,
0054              enum rcar_du_output output,
0055              struct device_node *enc_node)
0056 {
0057     struct rcar_du_encoder *renc;
0058     struct drm_connector *connector;
0059     struct drm_bridge *bridge;
0060     int ret;
0061 
0062     /*
0063      * Locate the DRM bridge from the DT node. For the DPAD outputs, if the
0064      * DT node has a single port, assume that it describes a panel and
0065      * create a panel bridge.
0066      */
0067     if ((output == RCAR_DU_OUTPUT_DPAD0 ||
0068          output == RCAR_DU_OUTPUT_DPAD1) &&
0069         rcar_du_encoder_count_ports(enc_node) == 1) {
0070         struct drm_panel *panel = of_drm_find_panel(enc_node);
0071 
0072         if (IS_ERR(panel))
0073             return PTR_ERR(panel);
0074 
0075         bridge = devm_drm_panel_bridge_add_typed(rcdu->dev, panel,
0076                              DRM_MODE_CONNECTOR_DPI);
0077         if (IS_ERR(bridge))
0078             return PTR_ERR(bridge);
0079     } else {
0080         bridge = of_drm_find_bridge(enc_node);
0081         if (!bridge)
0082             return -EPROBE_DEFER;
0083 
0084         if (output == RCAR_DU_OUTPUT_LVDS0 ||
0085             output == RCAR_DU_OUTPUT_LVDS1)
0086             rcdu->lvds[output - RCAR_DU_OUTPUT_LVDS0] = bridge;
0087     }
0088 
0089     /*
0090      * Create and initialize the encoder. On Gen3, skip the LVDS1 output if
0091      * the LVDS1 encoder is used as a companion for LVDS0 in dual-link
0092      * mode, or any LVDS output if it isn't connected. The latter may happen
0093      * on D3 or E3 as the LVDS encoders are needed to provide the pixel
0094      * clock to the DU, even when the LVDS outputs are not used.
0095      */
0096     if (rcdu->info->gen >= 3) {
0097         if (output == RCAR_DU_OUTPUT_LVDS1 &&
0098             rcar_lvds_dual_link(bridge))
0099             return -ENOLINK;
0100 
0101         if ((output == RCAR_DU_OUTPUT_LVDS0 ||
0102              output == RCAR_DU_OUTPUT_LVDS1) &&
0103             !rcar_lvds_is_connected(bridge))
0104             return -ENOLINK;
0105     }
0106 
0107     dev_dbg(rcdu->dev, "initializing encoder %pOF for output %s\n",
0108         enc_node, rcar_du_output_name(output));
0109 
0110     renc = drmm_encoder_alloc(&rcdu->ddev, struct rcar_du_encoder, base,
0111                   &rcar_du_encoder_funcs, DRM_MODE_ENCODER_NONE,
0112                   NULL);
0113     if (!renc)
0114         return -ENOMEM;
0115 
0116     renc->output = output;
0117 
0118     /* Attach the bridge to the encoder. */
0119     ret = drm_bridge_attach(&renc->base, bridge, NULL,
0120                 DRM_BRIDGE_ATTACH_NO_CONNECTOR);
0121     if (ret) {
0122         dev_err(rcdu->dev,
0123             "failed to attach bridge %pOF for output %s (%d)\n",
0124             bridge->of_node, rcar_du_output_name(output), ret);
0125         return ret;
0126     }
0127 
0128     /* Create the connector for the chain of bridges. */
0129     connector = drm_bridge_connector_init(&rcdu->ddev, &renc->base);
0130     if (IS_ERR(connector)) {
0131         dev_err(rcdu->dev,
0132             "failed to created connector for output %s (%ld)\n",
0133             rcar_du_output_name(output), PTR_ERR(connector));
0134         return PTR_ERR(connector);
0135     }
0136 
0137     return drm_connector_attach_encoder(connector, &renc->base);
0138 }