ARM: pmu: add support for interrupt-affinity property
[linux/fpc-iii.git] / drivers / gpu / drm / rcar-du / rcar_du_encoder.c
blob279167f783f67857ee3b37cd491419626933399d
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_hdmicon.h"
23 #include "rcar_du_hdmienc.h"
24 #include "rcar_du_kms.h"
25 #include "rcar_du_lvdscon.h"
26 #include "rcar_du_lvdsenc.h"
27 #include "rcar_du_vgacon.h"
29 /* -----------------------------------------------------------------------------
30 * Common connector functions
33 struct drm_encoder *
34 rcar_du_connector_best_encoder(struct drm_connector *connector)
36 struct rcar_du_connector *rcon = to_rcar_connector(connector);
38 return rcar_encoder_to_drm_encoder(rcon->encoder);
41 /* -----------------------------------------------------------------------------
42 * Encoder
45 static void rcar_du_encoder_dpms(struct drm_encoder *encoder, int mode)
47 struct rcar_du_encoder *renc = to_rcar_encoder(encoder);
49 if (mode != DRM_MODE_DPMS_ON)
50 mode = DRM_MODE_DPMS_OFF;
52 if (renc->lvds)
53 rcar_du_lvdsenc_dpms(renc->lvds, encoder->crtc, mode);
56 static bool rcar_du_encoder_mode_fixup(struct drm_encoder *encoder,
57 const struct drm_display_mode *mode,
58 struct drm_display_mode *adjusted_mode)
60 struct rcar_du_encoder *renc = to_rcar_encoder(encoder);
61 const struct drm_display_mode *panel_mode;
62 struct drm_device *dev = encoder->dev;
63 struct drm_connector *connector;
64 bool found = false;
66 /* DAC encoders have currently no restriction on the mode. */
67 if (encoder->encoder_type == DRM_MODE_ENCODER_DAC)
68 return true;
70 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
71 if (connector->encoder == encoder) {
72 found = true;
73 break;
77 if (!found) {
78 dev_dbg(dev->dev, "mode_fixup: no connector found\n");
79 return false;
82 if (list_empty(&connector->modes)) {
83 dev_dbg(dev->dev, "mode_fixup: empty modes list\n");
84 return false;
87 panel_mode = list_first_entry(&connector->modes,
88 struct drm_display_mode, head);
90 /* We're not allowed to modify the resolution. */
91 if (mode->hdisplay != panel_mode->hdisplay ||
92 mode->vdisplay != panel_mode->vdisplay)
93 return false;
95 /* The flat panel mode is fixed, just copy it to the adjusted mode. */
96 drm_mode_copy(adjusted_mode, panel_mode);
98 /* The internal LVDS encoder has a clock frequency operating range of
99 * 30MHz to 150MHz. Clamp the clock accordingly.
101 if (renc->lvds)
102 adjusted_mode->clock = clamp(adjusted_mode->clock,
103 30000, 150000);
105 return true;
108 static void rcar_du_encoder_mode_prepare(struct drm_encoder *encoder)
110 struct rcar_du_encoder *renc = to_rcar_encoder(encoder);
112 if (renc->lvds)
113 rcar_du_lvdsenc_dpms(renc->lvds, encoder->crtc,
114 DRM_MODE_DPMS_OFF);
117 static void rcar_du_encoder_mode_commit(struct drm_encoder *encoder)
119 struct rcar_du_encoder *renc = to_rcar_encoder(encoder);
121 if (renc->lvds)
122 rcar_du_lvdsenc_dpms(renc->lvds, encoder->crtc,
123 DRM_MODE_DPMS_ON);
126 static void rcar_du_encoder_mode_set(struct drm_encoder *encoder,
127 struct drm_display_mode *mode,
128 struct drm_display_mode *adjusted_mode)
130 struct rcar_du_encoder *renc = to_rcar_encoder(encoder);
132 rcar_du_crtc_route_output(encoder->crtc, renc->output);
135 static const struct drm_encoder_helper_funcs encoder_helper_funcs = {
136 .dpms = rcar_du_encoder_dpms,
137 .mode_fixup = rcar_du_encoder_mode_fixup,
138 .prepare = rcar_du_encoder_mode_prepare,
139 .commit = rcar_du_encoder_mode_commit,
140 .mode_set = rcar_du_encoder_mode_set,
143 static const struct drm_encoder_funcs encoder_funcs = {
144 .destroy = drm_encoder_cleanup,
147 int rcar_du_encoder_init(struct rcar_du_device *rcdu,
148 enum rcar_du_encoder_type type,
149 enum rcar_du_output output,
150 struct device_node *enc_node,
151 struct device_node *con_node)
153 struct rcar_du_encoder *renc;
154 struct drm_encoder *encoder;
155 unsigned int encoder_type;
156 int ret;
158 renc = devm_kzalloc(rcdu->dev, sizeof(*renc), GFP_KERNEL);
159 if (renc == NULL)
160 return -ENOMEM;
162 renc->output = output;
163 encoder = rcar_encoder_to_drm_encoder(renc);
165 switch (output) {
166 case RCAR_DU_OUTPUT_LVDS0:
167 renc->lvds = rcdu->lvds[0];
168 break;
170 case RCAR_DU_OUTPUT_LVDS1:
171 renc->lvds = rcdu->lvds[1];
172 break;
174 default:
175 break;
178 switch (type) {
179 case RCAR_DU_ENCODER_VGA:
180 encoder_type = DRM_MODE_ENCODER_DAC;
181 break;
182 case RCAR_DU_ENCODER_LVDS:
183 encoder_type = DRM_MODE_ENCODER_LVDS;
184 break;
185 case RCAR_DU_ENCODER_HDMI:
186 encoder_type = DRM_MODE_ENCODER_TMDS;
187 break;
188 case RCAR_DU_ENCODER_NONE:
189 default:
190 /* No external encoder, use the internal encoder type. */
191 encoder_type = rcdu->info->routes[output].encoder_type;
192 break;
195 if (type == RCAR_DU_ENCODER_HDMI) {
196 ret = rcar_du_hdmienc_init(rcdu, renc, enc_node);
197 if (ret < 0)
198 goto done;
199 } else {
200 ret = drm_encoder_init(rcdu->ddev, encoder, &encoder_funcs,
201 encoder_type);
202 if (ret < 0)
203 goto done;
205 drm_encoder_helper_add(encoder, &encoder_helper_funcs);
208 switch (encoder_type) {
209 case DRM_MODE_ENCODER_LVDS:
210 ret = rcar_du_lvds_connector_init(rcdu, renc, con_node);
211 break;
213 case DRM_MODE_ENCODER_DAC:
214 ret = rcar_du_vga_connector_init(rcdu, renc);
215 break;
217 case DRM_MODE_ENCODER_TMDS:
218 ret = rcar_du_hdmi_connector_init(rcdu, renc);
219 break;
221 default:
222 ret = -EINVAL;
223 break;
226 done:
227 if (ret < 0) {
228 if (encoder->name)
229 encoder->funcs->destroy(encoder);
230 devm_kfree(rcdu->dev, renc);
233 return ret;