Linux 4.9.243
[linux/fpc-iii.git] / drivers / gpu / drm / rcar-du / rcar_du_encoder.c
blobab8645c57e2d5f94a0a36b41910ef1db6861d638
1 /*
2 * rcar_du_encoder.c -- R-Car Display Unit Encoder
4 * Copyright (C) 2013-2014 Renesas Electronics Corporation
6 * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
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.
14 #include <linux/export.h>
16 #include <drm/drmP.h>
17 #include <drm/drm_crtc.h>
18 #include <drm/drm_crtc_helper.h>
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"
28 /* -----------------------------------------------------------------------------
29 * Encoder
32 static void rcar_du_encoder_disable(struct drm_encoder *encoder)
34 struct rcar_du_encoder *renc = to_rcar_encoder(encoder);
36 if (renc->lvds)
37 rcar_du_lvdsenc_enable(renc->lvds, encoder->crtc, false);
40 static void rcar_du_encoder_enable(struct drm_encoder *encoder)
42 struct rcar_du_encoder *renc = to_rcar_encoder(encoder);
44 if (renc->lvds)
45 rcar_du_lvdsenc_enable(renc->lvds, encoder->crtc, true);
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)
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;
59 /* DAC encoders have currently no restriction on the mode. */
60 if (encoder->encoder_type == DRM_MODE_ENCODER_DAC)
61 return 0;
63 if (list_empty(&connector->modes)) {
64 dev_dbg(dev->dev, "encoder: empty modes list\n");
65 return -EINVAL;
68 panel_mode = list_first_entry(&connector->modes,
69 struct drm_display_mode, head);
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;
76 /* The flat panel mode is fixed, just copy it to the adjusted mode. */
77 drm_mode_copy(adjusted_mode, panel_mode);
79 if (renc->lvds)
80 rcar_du_lvdsenc_atomic_check(renc->lvds, adjusted_mode);
82 return 0;
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)
89 struct rcar_du_encoder *renc = to_rcar_encoder(encoder);
91 rcar_du_crtc_route_output(encoder->crtc, renc->output);
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,
101 static const struct drm_encoder_funcs encoder_funcs = {
102 .destroy = drm_encoder_cleanup,
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)
111 struct rcar_du_encoder *renc;
112 struct drm_encoder *encoder;
113 unsigned int encoder_type;
114 int ret;
116 renc = devm_kzalloc(rcdu->dev, sizeof(*renc), GFP_KERNEL);
117 if (renc == NULL)
118 return -ENOMEM;
120 renc->output = output;
121 encoder = rcar_encoder_to_drm_encoder(renc);
123 switch (output) {
124 case RCAR_DU_OUTPUT_LVDS0:
125 renc->lvds = rcdu->lvds[0];
126 break;
128 case RCAR_DU_OUTPUT_LVDS1:
129 renc->lvds = rcdu->lvds[1];
130 break;
132 default:
133 break;
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;
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;
163 drm_encoder_helper_add(encoder, &encoder_helper_funcs);
166 switch (encoder_type) {
167 case DRM_MODE_ENCODER_LVDS:
168 ret = rcar_du_lvds_connector_init(rcdu, renc, con_node);
169 break;
171 case DRM_MODE_ENCODER_DAC:
172 ret = rcar_du_vga_connector_init(rcdu, renc);
173 break;
175 case DRM_MODE_ENCODER_TMDS:
176 /* connector managed by the bridge driver */
177 break;
179 default:
180 ret = -EINVAL;
181 break;
184 done:
185 if (ret < 0) {
186 if (encoder->name)
187 encoder->funcs->destroy(encoder);
188 devm_kfree(rcdu->dev, renc);
191 return ret;