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