blob: d0ae1e8009c6b8982ddaf8746893770a21634ce8 [file] [log] [blame]
Laurent Pinchart6978f122013-06-15 15:02:12 +02001/*
2 * rcar_du_encoder.c -- R-Car Display Unit Encoder
3 *
Laurent Pinchart36d50462014-02-06 18:13:52 +01004 * Copyright (C) 2013-2014 Renesas Electronics Corporation
Laurent Pinchart6978f122013-06-15 15:02:12 +02005 *
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
Laurent Pinchart90374b52013-06-17 13:48:27 +020014#include <linux/export.h>
15
Laurent Pinchart6978f122013-06-15 15:02:12 +020016#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"
Laurent Pinchart637e6192014-03-30 21:55:38 +020022#include "rcar_du_hdmicon.h"
23#include "rcar_du_hdmienc.h"
Laurent Pinchart6978f122013-06-15 15:02:12 +020024#include "rcar_du_kms.h"
25#include "rcar_du_lvdscon.h"
Laurent Pinchart90374b52013-06-17 13:48:27 +020026#include "rcar_du_lvdsenc.h"
Laurent Pinchart6978f122013-06-15 15:02:12 +020027#include "rcar_du_vgacon.h"
28
29/* -----------------------------------------------------------------------------
30 * Common connector functions
31 */
32
33struct drm_encoder *
34rcar_du_connector_best_encoder(struct drm_connector *connector)
35{
36 struct rcar_du_connector *rcon = to_rcar_connector(connector);
37
Laurent Pinchart4b96b702014-03-31 01:50:16 +020038 return rcar_encoder_to_drm_encoder(rcon->encoder);
Laurent Pinchart6978f122013-06-15 15:02:12 +020039}
40
41/* -----------------------------------------------------------------------------
42 * Encoder
43 */
44
Laurent Pinchartf6638b62015-02-20 14:05:21 +020045static void rcar_du_encoder_disable(struct drm_encoder *encoder)
Laurent Pinchart6978f122013-06-15 15:02:12 +020046{
Laurent Pinchart90374b52013-06-17 13:48:27 +020047 struct rcar_du_encoder *renc = to_rcar_encoder(encoder);
48
49 if (renc->lvds)
Laurent Pinchartf6638b62015-02-20 14:05:21 +020050 rcar_du_lvdsenc_enable(renc->lvds, encoder->crtc, false);
51}
52
53static void rcar_du_encoder_enable(struct drm_encoder *encoder)
54{
55 struct rcar_du_encoder *renc = to_rcar_encoder(encoder);
56
57 if (renc->lvds)
58 rcar_du_lvdsenc_enable(renc->lvds, encoder->crtc, true);
59}
60
Laurent Pinchartede77142015-02-22 21:02:39 +020061static int rcar_du_encoder_atomic_check(struct drm_encoder *encoder,
62 struct drm_crtc_state *crtc_state,
63 struct drm_connector_state *conn_state)
Laurent Pinchart6978f122013-06-15 15:02:12 +020064{
Laurent Pinchart90374b52013-06-17 13:48:27 +020065 struct rcar_du_encoder *renc = to_rcar_encoder(encoder);
Laurent Pinchartede77142015-02-22 21:02:39 +020066 struct drm_display_mode *adjusted_mode = &crtc_state->adjusted_mode;
67 const struct drm_display_mode *mode = &crtc_state->mode;
Laurent Pinchart6978f122013-06-15 15:02:12 +020068 const struct drm_display_mode *panel_mode;
Laurent Pinchartede77142015-02-22 21:02:39 +020069 struct drm_connector *connector = conn_state->connector;
Laurent Pinchart6978f122013-06-15 15:02:12 +020070 struct drm_device *dev = encoder->dev;
Laurent Pinchart6978f122013-06-15 15:02:12 +020071
72 /* DAC encoders have currently no restriction on the mode. */
73 if (encoder->encoder_type == DRM_MODE_ENCODER_DAC)
Laurent Pinchartede77142015-02-22 21:02:39 +020074 return 0;
Laurent Pinchart6978f122013-06-15 15:02:12 +020075
76 if (list_empty(&connector->modes)) {
Laurent Pinchartede77142015-02-22 21:02:39 +020077 dev_dbg(dev->dev, "encoder: empty modes list\n");
78 return -EINVAL;
Laurent Pinchart6978f122013-06-15 15:02:12 +020079 }
80
81 panel_mode = list_first_entry(&connector->modes,
82 struct drm_display_mode, head);
83
84 /* We're not allowed to modify the resolution. */
85 if (mode->hdisplay != panel_mode->hdisplay ||
86 mode->vdisplay != panel_mode->vdisplay)
Laurent Pinchartede77142015-02-22 21:02:39 +020087 return -EINVAL;
Laurent Pinchart6978f122013-06-15 15:02:12 +020088
89 /* The flat panel mode is fixed, just copy it to the adjusted mode. */
90 drm_mode_copy(adjusted_mode, panel_mode);
91
Laurent Pinchart90374b52013-06-17 13:48:27 +020092 /* The internal LVDS encoder has a clock frequency operating range of
93 * 30MHz to 150MHz. Clamp the clock accordingly.
94 */
95 if (renc->lvds)
96 adjusted_mode->clock = clamp(adjusted_mode->clock,
97 30000, 150000);
98
Laurent Pinchartede77142015-02-22 21:02:39 +020099 return 0;
Laurent Pinchart6978f122013-06-15 15:02:12 +0200100}
101
Laurent Pinchart6978f122013-06-15 15:02:12 +0200102static void rcar_du_encoder_mode_set(struct drm_encoder *encoder,
103 struct drm_display_mode *mode,
104 struct drm_display_mode *adjusted_mode)
105{
106 struct rcar_du_encoder *renc = to_rcar_encoder(encoder);
107
108 rcar_du_crtc_route_output(encoder->crtc, renc->output);
109}
110
111static const struct drm_encoder_helper_funcs encoder_helper_funcs = {
Laurent Pinchart6978f122013-06-15 15:02:12 +0200112 .mode_set = rcar_du_encoder_mode_set,
Laurent Pinchartf6638b62015-02-20 14:05:21 +0200113 .disable = rcar_du_encoder_disable,
114 .enable = rcar_du_encoder_enable,
Laurent Pinchartede77142015-02-22 21:02:39 +0200115 .atomic_check = rcar_du_encoder_atomic_check,
Laurent Pinchart6978f122013-06-15 15:02:12 +0200116};
117
118static const struct drm_encoder_funcs encoder_funcs = {
119 .destroy = drm_encoder_cleanup,
120};
121
122int rcar_du_encoder_init(struct rcar_du_device *rcdu,
Laurent Pinchartef67a902013-06-17 03:13:11 +0200123 enum rcar_du_encoder_type type,
124 enum rcar_du_output output,
Laurent Pinchart3ea4d5e2014-09-17 14:41:57 +0300125 struct device_node *enc_node,
126 struct device_node *con_node)
Laurent Pinchart6978f122013-06-15 15:02:12 +0200127{
128 struct rcar_du_encoder *renc;
Laurent Pinchart4b96b702014-03-31 01:50:16 +0200129 struct drm_encoder *encoder;
Laurent Pinchartef67a902013-06-17 03:13:11 +0200130 unsigned int encoder_type;
Laurent Pinchart6978f122013-06-15 15:02:12 +0200131 int ret;
132
133 renc = devm_kzalloc(rcdu->dev, sizeof(*renc), GFP_KERNEL);
134 if (renc == NULL)
135 return -ENOMEM;
136
137 renc->output = output;
Laurent Pinchart4b96b702014-03-31 01:50:16 +0200138 encoder = rcar_encoder_to_drm_encoder(renc);
Laurent Pinchart6978f122013-06-15 15:02:12 +0200139
Laurent Pinchart90374b52013-06-17 13:48:27 +0200140 switch (output) {
141 case RCAR_DU_OUTPUT_LVDS0:
142 renc->lvds = rcdu->lvds[0];
143 break;
144
145 case RCAR_DU_OUTPUT_LVDS1:
146 renc->lvds = rcdu->lvds[1];
147 break;
148
149 default:
150 break;
151 }
152
Laurent Pinchartef67a902013-06-17 03:13:11 +0200153 switch (type) {
154 case RCAR_DU_ENCODER_VGA:
155 encoder_type = DRM_MODE_ENCODER_DAC;
156 break;
157 case RCAR_DU_ENCODER_LVDS:
158 encoder_type = DRM_MODE_ENCODER_LVDS;
159 break;
Laurent Pinchart637e6192014-03-30 21:55:38 +0200160 case RCAR_DU_ENCODER_HDMI:
161 encoder_type = DRM_MODE_ENCODER_TMDS;
162 break;
Laurent Pinchartef67a902013-06-17 03:13:11 +0200163 case RCAR_DU_ENCODER_NONE:
164 default:
165 /* No external encoder, use the internal encoder type. */
166 encoder_type = rcdu->info->routes[output].encoder_type;
167 break;
168 }
169
Laurent Pinchart637e6192014-03-30 21:55:38 +0200170 if (type == RCAR_DU_ENCODER_HDMI) {
Laurent Pinchart637e6192014-03-30 21:55:38 +0200171 ret = rcar_du_hdmienc_init(rcdu, renc, enc_node);
172 if (ret < 0)
Laurent Pinchart347d7612014-12-11 01:26:04 +0200173 goto done;
Laurent Pinchart637e6192014-03-30 21:55:38 +0200174 } else {
175 ret = drm_encoder_init(rcdu->ddev, encoder, &encoder_funcs,
176 encoder_type);
177 if (ret < 0)
Laurent Pinchart347d7612014-12-11 01:26:04 +0200178 goto done;
Laurent Pinchart637e6192014-03-30 21:55:38 +0200179
180 drm_encoder_helper_add(encoder, &encoder_helper_funcs);
181 }
Laurent Pinchart6978f122013-06-15 15:02:12 +0200182
Laurent Pinchartef67a902013-06-17 03:13:11 +0200183 switch (encoder_type) {
Laurent Pinchart2378ad12014-09-17 02:07:52 +0300184 case DRM_MODE_ENCODER_LVDS:
Laurent Pinchart347d7612014-12-11 01:26:04 +0200185 ret = rcar_du_lvds_connector_init(rcdu, renc, con_node);
186 break;
Laurent Pinchart6978f122013-06-15 15:02:12 +0200187
Laurent Pinchartef67a902013-06-17 03:13:11 +0200188 case DRM_MODE_ENCODER_DAC:
Laurent Pinchart347d7612014-12-11 01:26:04 +0200189 ret = rcar_du_vga_connector_init(rcdu, renc);
190 break;
Laurent Pinchart6978f122013-06-15 15:02:12 +0200191
Laurent Pinchart637e6192014-03-30 21:55:38 +0200192 case DRM_MODE_ENCODER_TMDS:
Laurent Pinchart347d7612014-12-11 01:26:04 +0200193 ret = rcar_du_hdmi_connector_init(rcdu, renc);
194 break;
Laurent Pinchart637e6192014-03-30 21:55:38 +0200195
Laurent Pinchart6978f122013-06-15 15:02:12 +0200196 default:
Laurent Pinchart347d7612014-12-11 01:26:04 +0200197 ret = -EINVAL;
198 break;
Laurent Pinchart6978f122013-06-15 15:02:12 +0200199 }
Laurent Pinchart347d7612014-12-11 01:26:04 +0200200
201done:
202 if (ret < 0) {
203 if (encoder->name)
204 encoder->funcs->destroy(encoder);
205 devm_kfree(rcdu->dev, renc);
206 }
207
208 return ret;
Laurent Pinchart6978f122013-06-15 15:02:12 +0200209}