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