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
20 #include "rcar_du_drv.h"
21 #include "rcar_du_encoder.h"
22 #include "rcar_du_hdmienc.h"
23 #include "rcar_du_kms.h"
24 #include "rcar_du_lvdscon.h"
25 #include "rcar_du_lvdsenc.h"
26 #include "rcar_du_vgacon.h"
27
28 /* -----------------------------------------------------------------------------
29 * Encoder
30 */
31
rcar_du_encoder_disable(struct drm_encoder * encoder)32 static void rcar_du_encoder_disable(struct drm_encoder *encoder)
33 {
34 struct rcar_du_encoder *renc = to_rcar_encoder(encoder);
35
36 if (renc->lvds)
37 rcar_du_lvdsenc_enable(renc->lvds, encoder->crtc, false);
38 }
39
rcar_du_encoder_enable(struct drm_encoder * encoder)40 static void rcar_du_encoder_enable(struct drm_encoder *encoder)
41 {
42 struct rcar_du_encoder *renc = to_rcar_encoder(encoder);
43
44 if (renc->lvds)
45 rcar_du_lvdsenc_enable(renc->lvds, encoder->crtc, true);
46 }
47
rcar_du_encoder_atomic_check(struct drm_encoder * encoder,struct drm_crtc_state * crtc_state,struct drm_connector_state * conn_state)48 static int rcar_du_encoder_atomic_check(struct drm_encoder *encoder,
49 struct drm_crtc_state *crtc_state,
50 struct drm_connector_state *conn_state)
51 {
52 struct rcar_du_encoder *renc = to_rcar_encoder(encoder);
53 struct drm_display_mode *adjusted_mode = &crtc_state->adjusted_mode;
54 const struct drm_display_mode *mode = &crtc_state->mode;
55 const struct drm_display_mode *panel_mode;
56 struct drm_connector *connector = conn_state->connector;
57 struct drm_device *dev = encoder->dev;
58
59 /* DAC encoders have currently no restriction on the mode. */
60 if (encoder->encoder_type == DRM_MODE_ENCODER_DAC)
61 return 0;
62
63 if (list_empty(&connector->modes)) {
64 dev_dbg(dev->dev, "encoder: empty modes list\n");
65 return -EINVAL;
66 }
67
68 panel_mode = list_first_entry(&connector->modes,
69 struct drm_display_mode, head);
70
71 /* We're not allowed to modify the resolution. */
72 if (mode->hdisplay != panel_mode->hdisplay ||
73 mode->vdisplay != panel_mode->vdisplay)
74 return -EINVAL;
75
76 /* The flat panel mode is fixed, just copy it to the adjusted mode. */
77 drm_mode_copy(adjusted_mode, panel_mode);
78
79 if (renc->lvds)
80 rcar_du_lvdsenc_atomic_check(renc->lvds, adjusted_mode);
81
82 return 0;
83 }
84
rcar_du_encoder_mode_set(struct drm_encoder * encoder,struct drm_display_mode * mode,struct drm_display_mode * adjusted_mode)85 static void rcar_du_encoder_mode_set(struct drm_encoder *encoder,
86 struct drm_display_mode *mode,
87 struct drm_display_mode *adjusted_mode)
88 {
89 struct rcar_du_encoder *renc = to_rcar_encoder(encoder);
90
91 rcar_du_crtc_route_output(encoder->crtc, renc->output);
92 }
93
94 static const struct drm_encoder_helper_funcs encoder_helper_funcs = {
95 .mode_set = rcar_du_encoder_mode_set,
96 .disable = rcar_du_encoder_disable,
97 .enable = rcar_du_encoder_enable,
98 .atomic_check = rcar_du_encoder_atomic_check,
99 };
100
101 static const struct drm_encoder_funcs encoder_funcs = {
102 .destroy = drm_encoder_cleanup,
103 };
104
rcar_du_encoder_init(struct rcar_du_device * rcdu,enum rcar_du_encoder_type type,enum rcar_du_output output,struct device_node * enc_node,struct device_node * con_node)105 int rcar_du_encoder_init(struct rcar_du_device *rcdu,
106 enum rcar_du_encoder_type type,
107 enum rcar_du_output output,
108 struct device_node *enc_node,
109 struct device_node *con_node)
110 {
111 struct rcar_du_encoder *renc;
112 struct drm_encoder *encoder;
113 unsigned int encoder_type;
114 int ret;
115
116 renc = devm_kzalloc(rcdu->dev, sizeof(*renc), GFP_KERNEL);
117 if (renc == NULL)
118 return -ENOMEM;
119
120 renc->output = output;
121 encoder = rcar_encoder_to_drm_encoder(renc);
122
123 switch (output) {
124 case RCAR_DU_OUTPUT_LVDS0:
125 renc->lvds = rcdu->lvds[0];
126 break;
127
128 case RCAR_DU_OUTPUT_LVDS1:
129 renc->lvds = rcdu->lvds[1];
130 break;
131
132 default:
133 break;
134 }
135
136 switch (type) {
137 case RCAR_DU_ENCODER_VGA:
138 encoder_type = DRM_MODE_ENCODER_DAC;
139 break;
140 case RCAR_DU_ENCODER_LVDS:
141 encoder_type = DRM_MODE_ENCODER_LVDS;
142 break;
143 case RCAR_DU_ENCODER_HDMI:
144 encoder_type = DRM_MODE_ENCODER_TMDS;
145 break;
146 case RCAR_DU_ENCODER_NONE:
147 default:
148 /* No external encoder, use the internal encoder type. */
149 encoder_type = rcdu->info->routes[output].encoder_type;
150 break;
151 }
152
153 if (type == RCAR_DU_ENCODER_HDMI) {
154 ret = rcar_du_hdmienc_init(rcdu, renc, enc_node);
155 if (ret < 0)
156 goto done;
157 } else {
158 ret = drm_encoder_init(rcdu->ddev, encoder, &encoder_funcs,
159 encoder_type, NULL);
160 if (ret < 0)
161 goto done;
162
163 drm_encoder_helper_add(encoder, &encoder_helper_funcs);
164 }
165
166 switch (encoder_type) {
167 case DRM_MODE_ENCODER_LVDS:
168 ret = rcar_du_lvds_connector_init(rcdu, renc, con_node);
169 break;
170
171 case DRM_MODE_ENCODER_DAC:
172 ret = rcar_du_vga_connector_init(rcdu, renc);
173 break;
174
175 case DRM_MODE_ENCODER_TMDS:
176 /* connector managed by the bridge driver */
177 break;
178
179 default:
180 ret = -EINVAL;
181 break;
182 }
183
184 done:
185 if (ret < 0) {
186 if (encoder->name)
187 encoder->funcs->destroy(encoder);
188 devm_kfree(rcdu->dev, renc);
189 }
190
191 return ret;
192 }
193