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_bridge.h>
13#include <drm/drm_crtc.h>
14#include <drm/drm_modeset_helper_vtables.h>
15#include <drm/drm_panel.h>
16#include <drm/drm_simple_kms_helper.h>
17
18#include "rcar_du_drv.h"
19#include "rcar_du_encoder.h"
20#include "rcar_du_kms.h"
21#include "rcar_lvds.h"
22
23/* -----------------------------------------------------------------------------
24 * Encoder
25 */
26
27static unsigned int rcar_du_encoder_count_ports(struct device_node *node)
28{
29 struct device_node *ports;
30 struct device_node *port;
31 unsigned int num_ports = 0;
32
33 ports = of_get_child_by_name(node, "ports");
34 if (!ports)
35 ports = of_node_get(node);
36
37 for_each_child_of_node(ports, port) {
38 if (of_node_name_eq(port, "port"))
39 num_ports++;
40 }
41
42 of_node_put(ports);
43
44 return num_ports;
45}
46
47int rcar_du_encoder_init(struct rcar_du_device *rcdu,
48 enum rcar_du_output output,
49 struct device_node *enc_node)
50{
51 struct rcar_du_encoder *renc;
52 struct drm_encoder *encoder;
53 struct drm_bridge *bridge;
54 int ret;
55
56 renc = devm_kzalloc(rcdu->dev, sizeof(*renc), GFP_KERNEL);
57 if (renc == NULL)
58 return -ENOMEM;
59
60 rcdu->encoders[output] = renc;
61 renc->output = output;
62 encoder = rcar_encoder_to_drm_encoder(renc);
63
64 dev_dbg(rcdu->dev, "initializing encoder %pOF for output %u\n",
65 enc_node, output);
66
67 /*
68 * Locate the DRM bridge from the DT node. For the DPAD outputs, if the
69 * DT node has a single port, assume that it describes a panel and
70 * create a panel bridge.
71 */
72 if ((output == RCAR_DU_OUTPUT_DPAD0 ||
73 output == RCAR_DU_OUTPUT_DPAD1) &&
74 rcar_du_encoder_count_ports(enc_node) == 1) {
75 struct drm_panel *panel = of_drm_find_panel(enc_node);
76
77 if (IS_ERR(panel)) {
78 ret = PTR_ERR(panel);
79 goto done;
80 }
81
82 bridge = devm_drm_panel_bridge_add_typed(rcdu->dev, panel,
83 DRM_MODE_CONNECTOR_DPI);
84 if (IS_ERR(bridge)) {
85 ret = PTR_ERR(bridge);
86 goto done;
87 }
88 } else {
89 bridge = of_drm_find_bridge(enc_node);
90 if (!bridge) {
91 ret = -EPROBE_DEFER;
92 goto done;
93 }
94 }
95
96 /*
97 * On Gen3 skip the LVDS1 output if the LVDS1 encoder is used as a
98 * companion for LVDS0 in dual-link mode.
99 */
100 if (rcdu->info->gen >= 3 && output == RCAR_DU_OUTPUT_LVDS1) {
101 if (rcar_lvds_dual_link(bridge)) {
102 ret = -ENOLINK;
103 goto done;
104 }
105 }
106
107 ret = drm_simple_encoder_init(rcdu->ddev, encoder,
108 DRM_MODE_ENCODER_NONE);
109 if (ret < 0)
110 goto done;
111
112 /*
113 * Attach the bridge to the encoder. The bridge will create the
114 * connector.
115 */
116 ret = drm_bridge_attach(encoder, bridge, NULL, 0);
117 if (ret) {
118 drm_encoder_cleanup(encoder);
119 return ret;
120 }
121
122done:
123 if (ret < 0) {
124 if (encoder->name)
125 encoder->funcs->destroy(encoder);
126 devm_kfree(rcdu->dev, renc);
127 }
128
129 return ret;
130}
1/*
2 * rcar_du_encoder.c -- R-Car Display Unit Encoder
3 *
4 * Copyright (C) 2013-2014 Renesas Electronics Corporation
5 *
6 * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 */
13
14#include <linux/export.h>
15
16#include <drm/drmP.h>
17#include <drm/drm_crtc.h>
18#include <drm/drm_crtc_helper.h>
19#include <drm/drm_panel.h>
20
21#include "rcar_du_drv.h"
22#include "rcar_du_encoder.h"
23#include "rcar_du_kms.h"
24
25/* -----------------------------------------------------------------------------
26 * Encoder
27 */
28
29static void rcar_du_encoder_mode_set(struct drm_encoder *encoder,
30 struct drm_crtc_state *crtc_state,
31 struct drm_connector_state *conn_state)
32{
33 struct rcar_du_encoder *renc = to_rcar_encoder(encoder);
34
35 rcar_du_crtc_route_output(crtc_state->crtc, renc->output);
36}
37
38static const struct drm_encoder_helper_funcs encoder_helper_funcs = {
39 .atomic_mode_set = rcar_du_encoder_mode_set,
40};
41
42static const struct drm_encoder_funcs encoder_funcs = {
43 .destroy = drm_encoder_cleanup,
44};
45
46int rcar_du_encoder_init(struct rcar_du_device *rcdu,
47 enum rcar_du_output output,
48 struct device_node *enc_node,
49 struct device_node *con_node)
50{
51 struct rcar_du_encoder *renc;
52 struct drm_encoder *encoder;
53 struct drm_bridge *bridge = NULL;
54 int ret;
55
56 renc = devm_kzalloc(rcdu->dev, sizeof(*renc), GFP_KERNEL);
57 if (renc == NULL)
58 return -ENOMEM;
59
60 renc->output = output;
61 encoder = rcar_encoder_to_drm_encoder(renc);
62
63 dev_dbg(rcdu->dev, "initializing encoder %pOF for output %u\n",
64 enc_node, output);
65
66 /* Locate the DRM bridge from the encoder DT node. */
67 bridge = of_drm_find_bridge(enc_node);
68 if (!bridge) {
69 ret = -EPROBE_DEFER;
70 goto done;
71 }
72
73 ret = drm_encoder_init(rcdu->ddev, encoder, &encoder_funcs,
74 DRM_MODE_ENCODER_NONE, NULL);
75 if (ret < 0)
76 goto done;
77
78 drm_encoder_helper_add(encoder, &encoder_helper_funcs);
79
80 /*
81 * Attach the bridge to the encoder. The bridge will create the
82 * connector.
83 */
84 ret = drm_bridge_attach(encoder, bridge, NULL);
85 if (ret) {
86 drm_encoder_cleanup(encoder);
87 return ret;
88 }
89
90done:
91 if (ret < 0) {
92 if (encoder->name)
93 encoder->funcs->destroy(encoder);
94 devm_kfree(rcdu->dev, renc);
95 }
96
97 return ret;
98}