blob: 9eb3db9ba23f01729b649d2236f935a9875c50e2 [file] [log] [blame]
Rob Clarkcd5351f2011-11-12 12:09:40 -06001/*
Andrew F. Davisbb5cdf82017-12-05 14:29:31 -06002 * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
Rob Clarkcd5351f2011-11-12 12:09:40 -06003 * Author: Rob Clark <rob@ti.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
Laurent Pinchart2d278f52015-03-05 21:31:37 +020018#include <linux/list.h>
19
20#include <drm/drm_crtc.h>
Daniel Vetterfcd70cd2019-01-17 22:03:34 +010021#include <drm/drm_modeset_helper_vtables.h>
Tomi Valkeinen4f930c02014-06-18 14:19:48 +030022#include <drm/drm_edid.h>
23
Rob Clarkcd5351f2011-11-12 12:09:40 -060024#include "omap_drv.h"
25
Rob Clarkcd5351f2011-11-12 12:09:40 -060026/*
27 * encoder funcs
28 */
29
30#define to_omap_encoder(x) container_of(x, struct omap_encoder, base)
31
Rob Clarkf5f94542012-12-04 13:59:12 -060032/* The encoder and connector both map to same dssdev.. the encoder
33 * handles the 'active' parts, ie. anything the modifies the state
34 * of the hw, and the connector handles the 'read-only' parts, like
35 * detecting connection and reading edid.
36 */
Rob Clarkcd5351f2011-11-12 12:09:40 -060037struct omap_encoder {
38 struct drm_encoder base;
Laurent Pinchartd96aaad2018-05-31 23:14:43 +030039 struct omap_dss_device *output;
Rob Clarkcd5351f2011-11-12 12:09:40 -060040};
41
42static void omap_encoder_destroy(struct drm_encoder *encoder)
43{
44 struct omap_encoder *omap_encoder = to_omap_encoder(encoder);
Archit Tanejaec72a812014-01-02 14:49:53 +053045
Rob Clarkcd5351f2011-11-12 12:09:40 -060046 drm_encoder_cleanup(encoder);
47 kfree(omap_encoder);
48}
49
Rob Clarkf5f94542012-12-04 13:59:12 -060050static const struct drm_encoder_funcs omap_encoder_funcs = {
51 .destroy = omap_encoder_destroy,
52};
53
Laurent Pinchart79107f22018-09-23 12:58:15 +030054static void omap_encoder_update_videomode_flags(struct videomode *vm,
55 u32 bus_flags)
56{
57 if (!(vm->flags & (DISPLAY_FLAGS_DE_LOW |
58 DISPLAY_FLAGS_DE_HIGH))) {
59 if (bus_flags & DRM_BUS_FLAG_DE_LOW)
60 vm->flags |= DISPLAY_FLAGS_DE_LOW;
61 else if (bus_flags & DRM_BUS_FLAG_DE_HIGH)
62 vm->flags |= DISPLAY_FLAGS_DE_HIGH;
63 }
64
65 if (!(vm->flags & (DISPLAY_FLAGS_PIXDATA_POSEDGE |
66 DISPLAY_FLAGS_PIXDATA_NEGEDGE))) {
67 if (bus_flags & DRM_BUS_FLAG_PIXDATA_DRIVE_POSEDGE)
68 vm->flags |= DISPLAY_FLAGS_PIXDATA_POSEDGE;
69 else if (bus_flags & DRM_BUS_FLAG_PIXDATA_DRIVE_NEGEDGE)
70 vm->flags |= DISPLAY_FLAGS_PIXDATA_NEGEDGE;
71 }
72
73 if (!(vm->flags & (DISPLAY_FLAGS_SYNC_POSEDGE |
74 DISPLAY_FLAGS_SYNC_NEGEDGE))) {
75 if (bus_flags & DRM_BUS_FLAG_SYNC_DRIVE_POSEDGE)
76 vm->flags |= DISPLAY_FLAGS_SYNC_POSEDGE;
77 else if (bus_flags & DRM_BUS_FLAG_SYNC_DRIVE_NEGEDGE)
78 vm->flags |= DISPLAY_FLAGS_SYNC_NEGEDGE;
79 }
80}
81
Sebastian Reichel3c613a32018-11-21 17:09:14 +010082static void omap_encoder_hdmi_mode_set(struct drm_encoder *encoder,
83 struct drm_display_mode *adjusted_mode)
84{
85 struct drm_device *dev = encoder->dev;
86 struct omap_encoder *omap_encoder = to_omap_encoder(encoder);
87 struct omap_dss_device *dssdev = omap_encoder->output;
88 struct drm_connector *connector;
89 bool hdmi_mode;
90
91 hdmi_mode = false;
92 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
93 if (connector->encoder == encoder) {
94 hdmi_mode = omap_connector_get_hdmi_mode(connector);
95 break;
96 }
97 }
98
99 if (dssdev->ops->hdmi.set_hdmi_mode)
100 dssdev->ops->hdmi.set_hdmi_mode(dssdev, hdmi_mode);
101
102 if (hdmi_mode && dssdev->ops->hdmi.set_infoframe) {
103 struct hdmi_avi_infoframe avi;
104 int r;
105
Maxime Ripard23d19ba2019-01-11 16:32:10 +0100106 r = drm_hdmi_avi_infoframe_from_display_mode(&avi, connector,
107 adjusted_mode);
Sebastian Reichel3c613a32018-11-21 17:09:14 +0100108 if (r == 0)
109 dssdev->ops->hdmi.set_infoframe(dssdev, &avi);
110 }
111}
112
Rob Clarkcd5351f2011-11-12 12:09:40 -0600113static void omap_encoder_mode_set(struct drm_encoder *encoder,
Laurent Pinchart3fbda312018-06-07 17:58:57 +0300114 struct drm_display_mode *mode,
115 struct drm_display_mode *adjusted_mode)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600116{
Tomi Valkeinen4f930c02014-06-18 14:19:48 +0300117 struct omap_encoder *omap_encoder = to_omap_encoder(encoder);
Laurent Pinchart79107f22018-09-23 12:58:15 +0300118 struct omap_dss_device *output = omap_encoder->output;
Laurent Pinchart3fbda312018-06-07 17:58:57 +0300119 struct omap_dss_device *dssdev;
Laurent Pinchart79107f22018-09-23 12:58:15 +0300120 struct drm_bridge *bridge;
Laurent Pinchart3fbda312018-06-07 17:58:57 +0300121 struct videomode vm = { 0 };
Tomi Valkeinen4f930c02014-06-18 14:19:48 +0300122
Laurent Pinchart3fbda312018-06-07 17:58:57 +0300123 drm_display_mode_to_videomode(adjusted_mode, &vm);
124
125 /*
126 * HACK: This fixes the vm flags.
127 * struct drm_display_mode does not contain the VSYNC/HSYNC/DE flags and
128 * they get lost when converting back and forth between struct
129 * drm_display_mode and struct videomode. The hack below goes and
130 * fetches the missing flags.
131 *
132 * A better solution is to use DRM's bus-flags through the whole driver.
133 */
Laurent Pinchart79107f22018-09-23 12:58:15 +0300134 for (dssdev = output; dssdev; dssdev = dssdev->next)
135 omap_encoder_update_videomode_flags(&vm, dssdev->bus_flags);
Laurent Pinchart3fbda312018-06-07 17:58:57 +0300136
Laurent Pinchart79107f22018-09-23 12:58:15 +0300137 for (bridge = output->bridge; bridge; bridge = bridge->next) {
138 u32 bus_flags;
Laurent Pinchart3fbda312018-06-07 17:58:57 +0300139
Laurent Pinchart79107f22018-09-23 12:58:15 +0300140 if (!bridge->timings)
141 continue;
Laurent Pinchart3fbda312018-06-07 17:58:57 +0300142
Laurent Pinchart79107f22018-09-23 12:58:15 +0300143 bus_flags = bridge->timings->input_bus_flags;
144 omap_encoder_update_videomode_flags(&vm, bus_flags);
Laurent Pinchart3fbda312018-06-07 17:58:57 +0300145 }
146
Laurent Pinchart6ea484302018-06-07 19:55:04 +0300147 /* Set timings for all devices in the display pipeline. */
Laurent Pinchart79107f22018-09-23 12:58:15 +0300148 dss_mgr_set_timings(output, &vm);
Laurent Pinchart3fbda312018-06-07 17:58:57 +0300149
Laurent Pinchart79107f22018-09-23 12:58:15 +0300150 for (dssdev = output; dssdev; dssdev = dssdev->next) {
Laurent Pinchart6ea484302018-06-07 19:55:04 +0300151 if (dssdev->ops->set_timings)
Laurent Pinchart41322aa2018-09-21 17:00:29 +0300152 dssdev->ops->set_timings(dssdev, adjusted_mode);
Laurent Pinchart6ea484302018-06-07 19:55:04 +0300153 }
154
155 /* Set the HDMI mode and HDMI infoframe if applicable. */
Laurent Pinchart79107f22018-09-23 12:58:15 +0300156 if (output->type == OMAP_DISPLAY_TYPE_HDMI)
Sebastian Reichel3c613a32018-11-21 17:09:14 +0100157 omap_encoder_hdmi_mode_set(encoder, adjusted_mode);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600158}
159
Laurent Pinchart68dc0392015-03-07 00:22:39 +0200160static void omap_encoder_disable(struct drm_encoder *encoder)
161{
Rob Clarkcd5351f2011-11-12 12:09:40 -0600162 struct omap_encoder *omap_encoder = to_omap_encoder(encoder);
Laurent Pinchart19b42002018-08-24 19:38:07 +0300163 struct omap_dss_device *dssdev = omap_encoder->output;
Laurent Pinchartb80bfc62018-09-04 17:22:27 +0300164 struct drm_device *dev = encoder->dev;
165
166 dev_dbg(dev->dev, "disable(%s)\n", dssdev->name);
167
Laurent Pinchart19b42002018-08-24 19:38:07 +0300168 /*
169 * Disable the chain of external devices, starting at the one at the
170 * internal encoder's output.
171 */
172 omapdss_device_disable(dssdev->next);
Laurent Pinchartb80bfc62018-09-04 17:22:27 +0300173
Laurent Pinchart19b42002018-08-24 19:38:07 +0300174 /*
175 * Disable the internal encoder. This will disable the DSS output. The
176 * DSI is treated as an exception as DSI pipelines still use the legacy
177 * flow where the pipeline output controls the encoder.
178 */
Laurent Pinchart0dbfc392018-12-10 14:00:38 +0200179 if (dssdev->type != OMAP_DISPLAY_TYPE_DSI) {
Laurent Pinchart19b42002018-08-24 19:38:07 +0300180 dssdev->ops->disable(dssdev);
181 dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
182 }
183
184 /*
185 * Perform the post-disable operations on the chain of external devices
186 * to complete the display pipeline disable.
187 */
188 omapdss_device_post_disable(dssdev->next);
Rob Clarkf5f94542012-12-04 13:59:12 -0600189}
190
Laurent Pinchart4029755e2015-05-28 02:34:05 +0300191static void omap_encoder_enable(struct drm_encoder *encoder)
192{
193 struct omap_encoder *omap_encoder = to_omap_encoder(encoder);
Laurent Pinchart19b42002018-08-24 19:38:07 +0300194 struct omap_dss_device *dssdev = omap_encoder->output;
Laurent Pinchartb80bfc62018-09-04 17:22:27 +0300195 struct drm_device *dev = encoder->dev;
Laurent Pinchart4029755e2015-05-28 02:34:05 +0300196
Laurent Pinchartb80bfc62018-09-04 17:22:27 +0300197 dev_dbg(dev->dev, "enable(%s)\n", dssdev->name);
198
Laurent Pinchart19b42002018-08-24 19:38:07 +0300199 /* Prepare the chain of external devices for pipeline enable. */
200 omapdss_device_pre_enable(dssdev->next);
201
202 /*
203 * Enable the internal encoder. This will enable the DSS output. The
204 * DSI is treated as an exception as DSI pipelines still use the legacy
205 * flow where the pipeline output controls the encoder.
206 */
Laurent Pinchart0dbfc392018-12-10 14:00:38 +0200207 if (dssdev->type != OMAP_DISPLAY_TYPE_DSI) {
Laurent Pinchart19b42002018-08-24 19:38:07 +0300208 dssdev->ops->enable(dssdev);
209 dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
Laurent Pinchartb49a2132018-09-04 23:53:34 +0300210 }
Laurent Pinchartb80bfc62018-09-04 17:22:27 +0300211
Laurent Pinchart19b42002018-08-24 19:38:07 +0300212 /*
213 * Enable the chain of external devices, starting at the one at the
214 * internal encoder's output.
215 */
216 omapdss_device_enable(dssdev->next);
Laurent Pinchart4029755e2015-05-28 02:34:05 +0300217}
218
219static int omap_encoder_atomic_check(struct drm_encoder *encoder,
220 struct drm_crtc_state *crtc_state,
221 struct drm_connector_state *conn_state)
222{
Laurent Pinchart3fbda312018-06-07 17:58:57 +0300223 struct omap_encoder *omap_encoder = to_omap_encoder(encoder);
Laurent Pinchartd68164f2018-09-21 16:13:00 +0300224 enum drm_mode_status status;
Laurent Pinchart3fbda312018-06-07 17:58:57 +0300225
Laurent Pinchartd68164f2018-09-21 16:13:00 +0300226 status = omap_connector_mode_fixup(omap_encoder->output,
227 &crtc_state->mode,
228 &crtc_state->adjusted_mode);
229 if (status != MODE_OK) {
230 dev_err(encoder->dev->dev, "invalid timings: %d\n", status);
231 return -EINVAL;
Laurent Pinchart3fbda312018-06-07 17:58:57 +0300232 }
233
Laurent Pinchartd68164f2018-09-21 16:13:00 +0300234 return 0;
Laurent Pinchart4029755e2015-05-28 02:34:05 +0300235}
236
237static const struct drm_encoder_helper_funcs omap_encoder_helper_funcs = {
238 .mode_set = omap_encoder_mode_set,
239 .disable = omap_encoder_disable,
240 .enable = omap_encoder_enable,
241 .atomic_check = omap_encoder_atomic_check,
242};
243
Rob Clarkcd5351f2011-11-12 12:09:40 -0600244/* initialize encoder */
245struct drm_encoder *omap_encoder_init(struct drm_device *dev,
Laurent Pinchart79d11e92018-09-13 02:23:26 +0300246 struct omap_dss_device *output)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600247{
248 struct drm_encoder *encoder = NULL;
249 struct omap_encoder *omap_encoder;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600250
251 omap_encoder = kzalloc(sizeof(*omap_encoder), GFP_KERNEL);
Joe Perches78110bb2013-02-11 09:41:29 -0800252 if (!omap_encoder)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600253 goto fail;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600254
Laurent Pinchartd96aaad2018-05-31 23:14:43 +0300255 omap_encoder->output = output;
Rob Clarkf5f94542012-12-04 13:59:12 -0600256
Rob Clarkcd5351f2011-11-12 12:09:40 -0600257 encoder = &omap_encoder->base;
258
259 drm_encoder_init(dev, encoder, &omap_encoder_funcs,
Ville Syrjälä13a3d912015-12-09 16:20:18 +0200260 DRM_MODE_ENCODER_TMDS, NULL);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600261 drm_encoder_helper_add(encoder, &omap_encoder_helper_funcs);
262
Rob Clarkcd5351f2011-11-12 12:09:40 -0600263 return encoder;
264
265fail:
YAMANE Toshiaki582bc282012-11-14 19:31:27 +0900266 if (encoder)
Rob Clark65b0bd02011-12-09 23:26:07 -0600267 omap_encoder_destroy(encoder);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600268
269 return NULL;
270}