blob: 1f4172f653b9e9b99b067f9df9bfee41c0c9e178 [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
Sebastian Reichel3c613a32018-11-21 17:09:14 +010054static void omap_encoder_hdmi_mode_set(struct drm_encoder *encoder,
55 struct drm_display_mode *adjusted_mode)
56{
57 struct drm_device *dev = encoder->dev;
58 struct omap_encoder *omap_encoder = to_omap_encoder(encoder);
59 struct omap_dss_device *dssdev = omap_encoder->output;
60 struct drm_connector *connector;
61 bool hdmi_mode;
62
63 hdmi_mode = false;
64 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
65 if (connector->encoder == encoder) {
66 hdmi_mode = omap_connector_get_hdmi_mode(connector);
67 break;
68 }
69 }
70
71 if (dssdev->ops->hdmi.set_hdmi_mode)
72 dssdev->ops->hdmi.set_hdmi_mode(dssdev, hdmi_mode);
73
74 if (hdmi_mode && dssdev->ops->hdmi.set_infoframe) {
75 struct hdmi_avi_infoframe avi;
76 int r;
77
Maxime Ripard23d19ba2019-01-11 16:32:10 +010078 r = drm_hdmi_avi_infoframe_from_display_mode(&avi, connector,
79 adjusted_mode);
Sebastian Reichel3c613a32018-11-21 17:09:14 +010080 if (r == 0)
81 dssdev->ops->hdmi.set_infoframe(dssdev, &avi);
82 }
83}
84
Rob Clarkcd5351f2011-11-12 12:09:40 -060085static void omap_encoder_mode_set(struct drm_encoder *encoder,
Laurent Pinchart3fbda312018-06-07 17:58:57 +030086 struct drm_display_mode *mode,
87 struct drm_display_mode *adjusted_mode)
Rob Clarkcd5351f2011-11-12 12:09:40 -060088{
Tomi Valkeinen4f930c02014-06-18 14:19:48 +030089 struct omap_encoder *omap_encoder = to_omap_encoder(encoder);
Laurent Pinchart3fbda312018-06-07 17:58:57 +030090 struct omap_dss_device *dssdev;
91 struct videomode vm = { 0 };
Tomi Valkeinen4f930c02014-06-18 14:19:48 +030092
Laurent Pinchart3fbda312018-06-07 17:58:57 +030093 drm_display_mode_to_videomode(adjusted_mode, &vm);
94
95 /*
96 * HACK: This fixes the vm flags.
97 * struct drm_display_mode does not contain the VSYNC/HSYNC/DE flags and
98 * they get lost when converting back and forth between struct
99 * drm_display_mode and struct videomode. The hack below goes and
100 * fetches the missing flags.
101 *
102 * A better solution is to use DRM's bus-flags through the whole driver.
103 */
104 for (dssdev = omap_encoder->output; dssdev; dssdev = dssdev->next) {
105 unsigned long bus_flags = dssdev->bus_flags;
106
107 if (!(vm.flags & (DISPLAY_FLAGS_DE_LOW |
108 DISPLAY_FLAGS_DE_HIGH))) {
109 if (bus_flags & DRM_BUS_FLAG_DE_LOW)
110 vm.flags |= DISPLAY_FLAGS_DE_LOW;
111 else if (bus_flags & DRM_BUS_FLAG_DE_HIGH)
112 vm.flags |= DISPLAY_FLAGS_DE_HIGH;
113 }
114
115 if (!(vm.flags & (DISPLAY_FLAGS_PIXDATA_POSEDGE |
116 DISPLAY_FLAGS_PIXDATA_NEGEDGE))) {
117 if (bus_flags & DRM_BUS_FLAG_PIXDATA_POSEDGE)
118 vm.flags |= DISPLAY_FLAGS_PIXDATA_POSEDGE;
119 else if (bus_flags & DRM_BUS_FLAG_PIXDATA_NEGEDGE)
120 vm.flags |= DISPLAY_FLAGS_PIXDATA_NEGEDGE;
121 }
122
123 if (!(vm.flags & (DISPLAY_FLAGS_SYNC_POSEDGE |
124 DISPLAY_FLAGS_SYNC_NEGEDGE))) {
125 if (bus_flags & DRM_BUS_FLAG_SYNC_POSEDGE)
126 vm.flags |= DISPLAY_FLAGS_SYNC_POSEDGE;
127 else if (bus_flags & DRM_BUS_FLAG_SYNC_NEGEDGE)
128 vm.flags |= DISPLAY_FLAGS_SYNC_NEGEDGE;
129 }
130 }
131
Laurent Pinchart6ea484302018-06-07 19:55:04 +0300132 /* Set timings for all devices in the display pipeline. */
133 dss_mgr_set_timings(omap_encoder->output, &vm);
Laurent Pinchart3fbda312018-06-07 17:58:57 +0300134
Laurent Pinchart6ea484302018-06-07 19:55:04 +0300135 for (dssdev = omap_encoder->output; dssdev; dssdev = dssdev->next) {
136 if (dssdev->ops->set_timings)
137 dssdev->ops->set_timings(dssdev, &vm);
138 }
139
140 /* Set the HDMI mode and HDMI infoframe if applicable. */
Sebastian Reichel3c613a32018-11-21 17:09:14 +0100141 if (omap_encoder->output->output_type == OMAP_DISPLAY_TYPE_HDMI)
142 omap_encoder_hdmi_mode_set(encoder, adjusted_mode);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600143}
144
Laurent Pinchart68dc0392015-03-07 00:22:39 +0200145static void omap_encoder_disable(struct drm_encoder *encoder)
146{
Rob Clarkcd5351f2011-11-12 12:09:40 -0600147 struct omap_encoder *omap_encoder = to_omap_encoder(encoder);
Laurent Pinchart19b42002018-08-24 19:38:07 +0300148 struct omap_dss_device *dssdev = omap_encoder->output;
Laurent Pinchartb80bfc62018-09-04 17:22:27 +0300149 struct drm_device *dev = encoder->dev;
150
151 dev_dbg(dev->dev, "disable(%s)\n", dssdev->name);
152
Laurent Pinchart19b42002018-08-24 19:38:07 +0300153 /*
154 * Disable the chain of external devices, starting at the one at the
155 * internal encoder's output.
156 */
157 omapdss_device_disable(dssdev->next);
Laurent Pinchartb80bfc62018-09-04 17:22:27 +0300158
Laurent Pinchart19b42002018-08-24 19:38:07 +0300159 /*
160 * Disable the internal encoder. This will disable the DSS output. The
161 * DSI is treated as an exception as DSI pipelines still use the legacy
162 * flow where the pipeline output controls the encoder.
163 */
164 if (dssdev->output_type != OMAP_DISPLAY_TYPE_DSI) {
165 dssdev->ops->disable(dssdev);
166 dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
167 }
168
169 /*
170 * Perform the post-disable operations on the chain of external devices
171 * to complete the display pipeline disable.
172 */
173 omapdss_device_post_disable(dssdev->next);
Rob Clarkf5f94542012-12-04 13:59:12 -0600174}
175
Laurent Pinchart4029755e2015-05-28 02:34:05 +0300176static void omap_encoder_enable(struct drm_encoder *encoder)
177{
178 struct omap_encoder *omap_encoder = to_omap_encoder(encoder);
Laurent Pinchart19b42002018-08-24 19:38:07 +0300179 struct omap_dss_device *dssdev = omap_encoder->output;
Laurent Pinchartb80bfc62018-09-04 17:22:27 +0300180 struct drm_device *dev = encoder->dev;
Laurent Pinchart4029755e2015-05-28 02:34:05 +0300181
Laurent Pinchartb80bfc62018-09-04 17:22:27 +0300182 dev_dbg(dev->dev, "enable(%s)\n", dssdev->name);
183
Laurent Pinchart19b42002018-08-24 19:38:07 +0300184 /* Prepare the chain of external devices for pipeline enable. */
185 omapdss_device_pre_enable(dssdev->next);
186
187 /*
188 * Enable the internal encoder. This will enable the DSS output. The
189 * DSI is treated as an exception as DSI pipelines still use the legacy
190 * flow where the pipeline output controls the encoder.
191 */
192 if (dssdev->output_type != OMAP_DISPLAY_TYPE_DSI) {
193 dssdev->ops->enable(dssdev);
194 dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
Laurent Pinchartb49a2132018-09-04 23:53:34 +0300195 }
Laurent Pinchartb80bfc62018-09-04 17:22:27 +0300196
Laurent Pinchart19b42002018-08-24 19:38:07 +0300197 /*
198 * Enable the chain of external devices, starting at the one at the
199 * internal encoder's output.
200 */
201 omapdss_device_enable(dssdev->next);
Laurent Pinchart4029755e2015-05-28 02:34:05 +0300202}
203
204static int omap_encoder_atomic_check(struct drm_encoder *encoder,
205 struct drm_crtc_state *crtc_state,
206 struct drm_connector_state *conn_state)
207{
Laurent Pinchart3fbda312018-06-07 17:58:57 +0300208 struct omap_encoder *omap_encoder = to_omap_encoder(encoder);
Laurent Pinchart7c27fa52018-06-05 01:57:09 +0300209 enum omap_channel channel = omap_encoder->output->dispc_channel;
Laurent Pinchart3fbda312018-06-07 17:58:57 +0300210 struct drm_device *dev = encoder->dev;
Laurent Pinchart7c27fa52018-06-05 01:57:09 +0300211 struct omap_drm_private *priv = dev->dev_private;
Laurent Pinchart3fbda312018-06-07 17:58:57 +0300212 struct omap_dss_device *dssdev;
213 struct videomode vm = { 0 };
214 int ret;
215
216 drm_display_mode_to_videomode(&crtc_state->mode, &vm);
217
Laurent Pinchart7c27fa52018-06-05 01:57:09 +0300218 ret = priv->dispc_ops->mgr_check_timings(priv->dispc, channel, &vm);
219 if (ret)
220 goto done;
221
Laurent Pinchart3fbda312018-06-07 17:58:57 +0300222 for (dssdev = omap_encoder->output; dssdev; dssdev = dssdev->next) {
223 if (!dssdev->ops->check_timings)
224 continue;
225
226 ret = dssdev->ops->check_timings(dssdev, &vm);
Laurent Pinchart7c27fa52018-06-05 01:57:09 +0300227 if (ret)
228 goto done;
Laurent Pinchart3fbda312018-06-07 17:58:57 +0300229 }
230
231 drm_display_mode_from_videomode(&vm, &crtc_state->adjusted_mode);
232
Laurent Pinchart7c27fa52018-06-05 01:57:09 +0300233done:
234 if (ret)
235 dev_err(dev->dev, "invalid timings: %d\n", ret);
236
237 return ret;
Laurent Pinchart4029755e2015-05-28 02:34:05 +0300238}
239
240static const struct drm_encoder_helper_funcs omap_encoder_helper_funcs = {
241 .mode_set = omap_encoder_mode_set,
242 .disable = omap_encoder_disable,
243 .enable = omap_encoder_enable,
244 .atomic_check = omap_encoder_atomic_check,
245};
246
Rob Clarkcd5351f2011-11-12 12:09:40 -0600247/* initialize encoder */
248struct drm_encoder *omap_encoder_init(struct drm_device *dev,
Laurent Pinchart79d11e92018-09-13 02:23:26 +0300249 struct omap_dss_device *output)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600250{
251 struct drm_encoder *encoder = NULL;
252 struct omap_encoder *omap_encoder;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600253
254 omap_encoder = kzalloc(sizeof(*omap_encoder), GFP_KERNEL);
Joe Perches78110bb2013-02-11 09:41:29 -0800255 if (!omap_encoder)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600256 goto fail;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600257
Laurent Pinchartd96aaad2018-05-31 23:14:43 +0300258 omap_encoder->output = output;
Rob Clarkf5f94542012-12-04 13:59:12 -0600259
Rob Clarkcd5351f2011-11-12 12:09:40 -0600260 encoder = &omap_encoder->base;
261
262 drm_encoder_init(dev, encoder, &omap_encoder_funcs,
Ville Syrjälä13a3d912015-12-09 16:20:18 +0200263 DRM_MODE_ENCODER_TMDS, NULL);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600264 drm_encoder_helper_add(encoder, &omap_encoder_helper_funcs);
265
Rob Clarkcd5351f2011-11-12 12:09:40 -0600266 return encoder;
267
268fail:
YAMANE Toshiaki582bc282012-11-14 19:31:27 +0900269 if (encoder)
Rob Clark65b0bd02011-12-09 23:26:07 -0600270 omap_encoder_destroy(encoder);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600271
272 return NULL;
273}