1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * R-Car Display Unit Encoder 4 * 5 * Copyright (C) 2013-2014 Renesas Electronics Corporation 6 * 7 * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com) 8 */ 9 10 #include <linux/export.h> 11 #include <linux/of.h> 12 13 #include <drm/drm_bridge.h> 14 #include <drm/drm_bridge_connector.h> 15 #include <drm/drm_panel.h> 16 17 #include "rcar_du_drv.h" 18 #include "rcar_du_encoder.h" 19 #include "rcar_lvds.h" 20 21 /* ----------------------------------------------------------------------------- 22 * Encoder 23 */ 24 25 static unsigned int rcar_du_encoder_count_ports(struct device_node *node) 26 { 27 struct device_node *ports; 28 struct device_node *port; 29 unsigned int num_ports = 0; 30 31 ports = of_get_child_by_name(node, "ports"); 32 if (!ports) 33 ports = of_node_get(node); 34 35 for_each_child_of_node(ports, port) { 36 if (of_node_name_eq(port, "port")) 37 num_ports++; 38 } 39 40 of_node_put(ports); 41 42 return num_ports; 43 } 44 45 static const struct drm_encoder_funcs rcar_du_encoder_funcs = { 46 }; 47 48 int rcar_du_encoder_init(struct rcar_du_device *rcdu, 49 enum rcar_du_output output, 50 struct device_node *enc_node) 51 { 52 struct rcar_du_encoder *renc; 53 struct drm_connector *connector; 54 struct drm_bridge *bridge __free(drm_bridge_put) = NULL; 55 int ret; 56 57 /* 58 * Locate the DRM bridge from the DT node. For the DPAD outputs, if the 59 * DT node has a single port, assume that it describes a panel and 60 * create a panel bridge. 61 */ 62 if ((output == RCAR_DU_OUTPUT_DPAD0 || 63 output == RCAR_DU_OUTPUT_DPAD1) && 64 rcar_du_encoder_count_ports(enc_node) == 1) { 65 struct drm_panel *panel = of_drm_find_panel(enc_node); 66 67 if (IS_ERR(panel)) 68 return PTR_ERR(panel); 69 70 bridge = devm_drm_panel_bridge_add_typed(rcdu->dev, panel, 71 DRM_MODE_CONNECTOR_DPI); 72 drm_panel_put(panel); 73 if (IS_ERR(bridge)) 74 return PTR_ERR(no_free_ptr(bridge)); 75 76 /* 77 * The reference taken by devm_drm_panel_bridge_add_typed() is 78 * released automatically. Take a second one for the __free() 79 * when this function will return. 80 */ 81 drm_bridge_get(bridge); 82 } else { 83 bridge = of_drm_find_and_get_bridge(enc_node); 84 if (!bridge) 85 return -EPROBE_DEFER; 86 87 if (output == RCAR_DU_OUTPUT_LVDS0 || 88 output == RCAR_DU_OUTPUT_LVDS1) 89 rcdu->lvds[output - RCAR_DU_OUTPUT_LVDS0] = 90 drm_bridge_get(bridge); 91 92 if (output == RCAR_DU_OUTPUT_DSI0 || 93 output == RCAR_DU_OUTPUT_DSI1) 94 rcdu->dsi[output - RCAR_DU_OUTPUT_DSI0] = 95 drm_bridge_get(bridge); 96 } 97 98 /* 99 * Create and initialize the encoder. On Gen3, skip the LVDS1 output if 100 * the LVDS1 encoder is used as a companion for LVDS0 in dual-link 101 * mode, or any LVDS output if it isn't connected. The latter may happen 102 * on D3 or E3 as the LVDS encoders are needed to provide the pixel 103 * clock to the DU, even when the LVDS outputs are not used. 104 */ 105 if (rcdu->info->gen >= 3) { 106 if (output == RCAR_DU_OUTPUT_LVDS1 && 107 rcar_lvds_dual_link(bridge)) 108 return -ENOLINK; 109 110 if ((output == RCAR_DU_OUTPUT_LVDS0 || 111 output == RCAR_DU_OUTPUT_LVDS1) && 112 !rcar_lvds_is_connected(bridge)) 113 return -ENOLINK; 114 } 115 116 dev_dbg(rcdu->dev, "initializing encoder %pOF for output %s\n", 117 enc_node, rcar_du_output_name(output)); 118 119 renc = drmm_encoder_alloc(&rcdu->ddev, struct rcar_du_encoder, base, 120 &rcar_du_encoder_funcs, DRM_MODE_ENCODER_NONE, 121 NULL); 122 if (IS_ERR(renc)) 123 return PTR_ERR(renc); 124 125 renc->output = output; 126 127 /* Attach the bridge to the encoder. */ 128 ret = drm_bridge_attach(&renc->base, bridge, NULL, 129 DRM_BRIDGE_ATTACH_NO_CONNECTOR); 130 if (ret) { 131 dev_err(rcdu->dev, 132 "failed to attach bridge %pOF for output %s (%d)\n", 133 bridge->of_node, rcar_du_output_name(output), ret); 134 return ret; 135 } 136 137 /* Create the connector for the chain of bridges. */ 138 connector = drm_bridge_connector_init(&rcdu->ddev, &renc->base); 139 if (IS_ERR(connector)) { 140 dev_err(rcdu->dev, 141 "failed to created connector for output %s (%ld)\n", 142 rcar_du_output_name(output), PTR_ERR(connector)); 143 return PTR_ERR(connector); 144 } 145 146 return 0; 147 } 148 149 void rcar_du_encoder_cleanup(struct rcar_du_device *rcdu) 150 { 151 unsigned int i; 152 153 for (i = 0; i < ARRAY_SIZE(rcdu->lvds); i++) 154 drm_bridge_put(rcdu->lvds[i]); 155 for (i = 0; i < ARRAY_SIZE(rcdu->dsi); i++) 156 drm_bridge_put(rcdu->dsi[i]); 157 } 158