blob: 40512419642bc54d4e0490423332f87a16b7cb82 [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>
Laurent Pinchart30b71762018-12-07 23:08:35 +020023#include <drm/drm_panel.h>
Tomi Valkeinen4f930c02014-06-18 14:19:48 +030024
Rob Clarkcd5351f2011-11-12 12:09:40 -060025#include "omap_drv.h"
26
Rob Clarkcd5351f2011-11-12 12:09:40 -060027/*
28 * encoder funcs
29 */
30
31#define to_omap_encoder(x) container_of(x, struct omap_encoder, base)
32
Rob Clarkf5f94542012-12-04 13:59:12 -060033/* The encoder and connector both map to same dssdev.. the encoder
34 * handles the 'active' parts, ie. anything the modifies the state
35 * of the hw, and the connector handles the 'read-only' parts, like
36 * detecting connection and reading edid.
37 */
Rob Clarkcd5351f2011-11-12 12:09:40 -060038struct omap_encoder {
39 struct drm_encoder base;
Laurent Pinchartd96aaad2018-05-31 23:14:43 +030040 struct omap_dss_device *output;
Rob Clarkcd5351f2011-11-12 12:09:40 -060041};
42
43static void omap_encoder_destroy(struct drm_encoder *encoder)
44{
45 struct omap_encoder *omap_encoder = to_omap_encoder(encoder);
Archit Tanejaec72a812014-01-02 14:49:53 +053046
Rob Clarkcd5351f2011-11-12 12:09:40 -060047 drm_encoder_cleanup(encoder);
48 kfree(omap_encoder);
49}
50
Rob Clarkf5f94542012-12-04 13:59:12 -060051static const struct drm_encoder_funcs omap_encoder_funcs = {
52 .destroy = omap_encoder_destroy,
53};
54
Laurent Pinchart79107f22018-09-23 12:58:15 +030055static void omap_encoder_update_videomode_flags(struct videomode *vm,
56 u32 bus_flags)
57{
58 if (!(vm->flags & (DISPLAY_FLAGS_DE_LOW |
59 DISPLAY_FLAGS_DE_HIGH))) {
60 if (bus_flags & DRM_BUS_FLAG_DE_LOW)
61 vm->flags |= DISPLAY_FLAGS_DE_LOW;
62 else if (bus_flags & DRM_BUS_FLAG_DE_HIGH)
63 vm->flags |= DISPLAY_FLAGS_DE_HIGH;
64 }
65
66 if (!(vm->flags & (DISPLAY_FLAGS_PIXDATA_POSEDGE |
67 DISPLAY_FLAGS_PIXDATA_NEGEDGE))) {
68 if (bus_flags & DRM_BUS_FLAG_PIXDATA_DRIVE_POSEDGE)
69 vm->flags |= DISPLAY_FLAGS_PIXDATA_POSEDGE;
70 else if (bus_flags & DRM_BUS_FLAG_PIXDATA_DRIVE_NEGEDGE)
71 vm->flags |= DISPLAY_FLAGS_PIXDATA_NEGEDGE;
72 }
73
74 if (!(vm->flags & (DISPLAY_FLAGS_SYNC_POSEDGE |
75 DISPLAY_FLAGS_SYNC_NEGEDGE))) {
76 if (bus_flags & DRM_BUS_FLAG_SYNC_DRIVE_POSEDGE)
77 vm->flags |= DISPLAY_FLAGS_SYNC_POSEDGE;
78 else if (bus_flags & DRM_BUS_FLAG_SYNC_DRIVE_NEGEDGE)
79 vm->flags |= DISPLAY_FLAGS_SYNC_NEGEDGE;
80 }
81}
82
Laurent Pinchart30b71762018-12-07 23:08:35 +020083static void omap_encoder_hdmi_mode_set(struct drm_connector *connector,
84 struct drm_encoder *encoder,
Sebastian Reichel3c613a32018-11-21 17:09:14 +010085 struct drm_display_mode *adjusted_mode)
86{
Sebastian Reichel3c613a32018-11-21 17:09:14 +010087 struct omap_encoder *omap_encoder = to_omap_encoder(encoder);
88 struct omap_dss_device *dssdev = omap_encoder->output;
Sebastian Reichel3c613a32018-11-21 17:09:14 +010089 bool hdmi_mode;
90
Laurent Pinchart30b71762018-12-07 23:08:35 +020091 hdmi_mode = omap_connector_get_hdmi_mode(connector);
Sebastian Reichel3c613a32018-11-21 17:09:14 +010092
93 if (dssdev->ops->hdmi.set_hdmi_mode)
94 dssdev->ops->hdmi.set_hdmi_mode(dssdev, hdmi_mode);
95
96 if (hdmi_mode && dssdev->ops->hdmi.set_infoframe) {
97 struct hdmi_avi_infoframe avi;
98 int r;
99
Maxime Ripard23d19ba2019-01-11 16:32:10 +0100100 r = drm_hdmi_avi_infoframe_from_display_mode(&avi, connector,
101 adjusted_mode);
Sebastian Reichel3c613a32018-11-21 17:09:14 +0100102 if (r == 0)
103 dssdev->ops->hdmi.set_infoframe(dssdev, &avi);
104 }
105}
106
Rob Clarkcd5351f2011-11-12 12:09:40 -0600107static void omap_encoder_mode_set(struct drm_encoder *encoder,
Laurent Pinchart3fbda312018-06-07 17:58:57 +0300108 struct drm_display_mode *mode,
109 struct drm_display_mode *adjusted_mode)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600110{
Tomi Valkeinen4f930c02014-06-18 14:19:48 +0300111 struct omap_encoder *omap_encoder = to_omap_encoder(encoder);
Laurent Pinchart79107f22018-09-23 12:58:15 +0300112 struct omap_dss_device *output = omap_encoder->output;
Laurent Pinchart3fbda312018-06-07 17:58:57 +0300113 struct omap_dss_device *dssdev;
Laurent Pinchart30b71762018-12-07 23:08:35 +0200114 struct drm_device *dev = encoder->dev;
115 struct drm_connector *connector;
Laurent Pinchart79107f22018-09-23 12:58:15 +0300116 struct drm_bridge *bridge;
Laurent Pinchart3fbda312018-06-07 17:58:57 +0300117 struct videomode vm = { 0 };
Laurent Pinchart30b71762018-12-07 23:08:35 +0200118 u32 bus_flags;
119
120 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
121 if (connector->encoder == encoder)
122 break;
123 }
Tomi Valkeinen4f930c02014-06-18 14:19:48 +0300124
Laurent Pinchart3fbda312018-06-07 17:58:57 +0300125 drm_display_mode_to_videomode(adjusted_mode, &vm);
126
127 /*
128 * HACK: This fixes the vm flags.
129 * struct drm_display_mode does not contain the VSYNC/HSYNC/DE flags and
130 * they get lost when converting back and forth between struct
131 * drm_display_mode and struct videomode. The hack below goes and
132 * fetches the missing flags.
133 *
134 * A better solution is to use DRM's bus-flags through the whole driver.
135 */
Laurent Pinchart79107f22018-09-23 12:58:15 +0300136 for (dssdev = output; dssdev; dssdev = dssdev->next)
137 omap_encoder_update_videomode_flags(&vm, dssdev->bus_flags);
Laurent Pinchart3fbda312018-06-07 17:58:57 +0300138
Laurent Pinchart79107f22018-09-23 12:58:15 +0300139 for (bridge = output->bridge; bridge; bridge = bridge->next) {
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 Pinchart30b71762018-12-07 23:08:35 +0200147 bus_flags = connector->display_info.bus_flags;
148 omap_encoder_update_videomode_flags(&vm, bus_flags);
149
Laurent Pinchart6ea484302018-06-07 19:55:04 +0300150 /* Set timings for all devices in the display pipeline. */
Laurent Pinchart79107f22018-09-23 12:58:15 +0300151 dss_mgr_set_timings(output, &vm);
Laurent Pinchart3fbda312018-06-07 17:58:57 +0300152
Laurent Pinchart79107f22018-09-23 12:58:15 +0300153 for (dssdev = output; dssdev; dssdev = dssdev->next) {
Laurent Pinchart6ea484302018-06-07 19:55:04 +0300154 if (dssdev->ops->set_timings)
Laurent Pinchart41322aa2018-09-21 17:00:29 +0300155 dssdev->ops->set_timings(dssdev, adjusted_mode);
Laurent Pinchart6ea484302018-06-07 19:55:04 +0300156 }
157
158 /* Set the HDMI mode and HDMI infoframe if applicable. */
Laurent Pinchart79107f22018-09-23 12:58:15 +0300159 if (output->type == OMAP_DISPLAY_TYPE_HDMI)
Laurent Pinchart30b71762018-12-07 23:08:35 +0200160 omap_encoder_hdmi_mode_set(connector, encoder, adjusted_mode);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600161}
162
Laurent Pinchart68dc0392015-03-07 00:22:39 +0200163static void omap_encoder_disable(struct drm_encoder *encoder)
164{
Rob Clarkcd5351f2011-11-12 12:09:40 -0600165 struct omap_encoder *omap_encoder = to_omap_encoder(encoder);
Laurent Pinchart19b42002018-08-24 19:38:07 +0300166 struct omap_dss_device *dssdev = omap_encoder->output;
Laurent Pinchartb80bfc62018-09-04 17:22:27 +0300167 struct drm_device *dev = encoder->dev;
168
169 dev_dbg(dev->dev, "disable(%s)\n", dssdev->name);
170
Laurent Pinchart30b71762018-12-07 23:08:35 +0200171 /* Disable the panel if present. */
172 if (dssdev->panel) {
173 drm_panel_disable(dssdev->panel);
174 drm_panel_unprepare(dssdev->panel);
175 }
176
Laurent Pinchart19b42002018-08-24 19:38:07 +0300177 /*
178 * Disable the chain of external devices, starting at the one at the
179 * internal encoder's output.
180 */
181 omapdss_device_disable(dssdev->next);
Laurent Pinchartb80bfc62018-09-04 17:22:27 +0300182
Laurent Pinchart19b42002018-08-24 19:38:07 +0300183 /*
184 * Disable the internal encoder. This will disable the DSS output. The
185 * DSI is treated as an exception as DSI pipelines still use the legacy
186 * flow where the pipeline output controls the encoder.
187 */
Laurent Pinchart0dbfc392018-12-10 14:00:38 +0200188 if (dssdev->type != OMAP_DISPLAY_TYPE_DSI) {
Laurent Pinchart19b42002018-08-24 19:38:07 +0300189 dssdev->ops->disable(dssdev);
190 dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
191 }
192
193 /*
194 * Perform the post-disable operations on the chain of external devices
195 * to complete the display pipeline disable.
196 */
197 omapdss_device_post_disable(dssdev->next);
Rob Clarkf5f94542012-12-04 13:59:12 -0600198}
199
Laurent Pinchart4029755e2015-05-28 02:34:05 +0300200static void omap_encoder_enable(struct drm_encoder *encoder)
201{
202 struct omap_encoder *omap_encoder = to_omap_encoder(encoder);
Laurent Pinchart19b42002018-08-24 19:38:07 +0300203 struct omap_dss_device *dssdev = omap_encoder->output;
Laurent Pinchartb80bfc62018-09-04 17:22:27 +0300204 struct drm_device *dev = encoder->dev;
Laurent Pinchart4029755e2015-05-28 02:34:05 +0300205
Laurent Pinchartb80bfc62018-09-04 17:22:27 +0300206 dev_dbg(dev->dev, "enable(%s)\n", dssdev->name);
207
Laurent Pinchart19b42002018-08-24 19:38:07 +0300208 /* Prepare the chain of external devices for pipeline enable. */
209 omapdss_device_pre_enable(dssdev->next);
210
211 /*
212 * Enable the internal encoder. This will enable the DSS output. The
213 * DSI is treated as an exception as DSI pipelines still use the legacy
214 * flow where the pipeline output controls the encoder.
215 */
Laurent Pinchart0dbfc392018-12-10 14:00:38 +0200216 if (dssdev->type != OMAP_DISPLAY_TYPE_DSI) {
Laurent Pinchart19b42002018-08-24 19:38:07 +0300217 dssdev->ops->enable(dssdev);
218 dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
Laurent Pinchartb49a2132018-09-04 23:53:34 +0300219 }
Laurent Pinchartb80bfc62018-09-04 17:22:27 +0300220
Laurent Pinchart19b42002018-08-24 19:38:07 +0300221 /*
222 * Enable the chain of external devices, starting at the one at the
223 * internal encoder's output.
224 */
225 omapdss_device_enable(dssdev->next);
Laurent Pinchart30b71762018-12-07 23:08:35 +0200226
227 /* Enable the panel if present. */
228 if (dssdev->panel) {
229 drm_panel_prepare(dssdev->panel);
230 drm_panel_enable(dssdev->panel);
231 }
Laurent Pinchart4029755e2015-05-28 02:34:05 +0300232}
233
234static int omap_encoder_atomic_check(struct drm_encoder *encoder,
235 struct drm_crtc_state *crtc_state,
236 struct drm_connector_state *conn_state)
237{
Laurent Pinchart3fbda312018-06-07 17:58:57 +0300238 struct omap_encoder *omap_encoder = to_omap_encoder(encoder);
Laurent Pinchartd68164f2018-09-21 16:13:00 +0300239 enum drm_mode_status status;
Laurent Pinchart3fbda312018-06-07 17:58:57 +0300240
Laurent Pinchartd68164f2018-09-21 16:13:00 +0300241 status = omap_connector_mode_fixup(omap_encoder->output,
242 &crtc_state->mode,
243 &crtc_state->adjusted_mode);
244 if (status != MODE_OK) {
245 dev_err(encoder->dev->dev, "invalid timings: %d\n", status);
246 return -EINVAL;
Laurent Pinchart3fbda312018-06-07 17:58:57 +0300247 }
248
Laurent Pinchartd68164f2018-09-21 16:13:00 +0300249 return 0;
Laurent Pinchart4029755e2015-05-28 02:34:05 +0300250}
251
252static const struct drm_encoder_helper_funcs omap_encoder_helper_funcs = {
253 .mode_set = omap_encoder_mode_set,
254 .disable = omap_encoder_disable,
255 .enable = omap_encoder_enable,
256 .atomic_check = omap_encoder_atomic_check,
257};
258
Rob Clarkcd5351f2011-11-12 12:09:40 -0600259/* initialize encoder */
260struct drm_encoder *omap_encoder_init(struct drm_device *dev,
Laurent Pinchart79d11e92018-09-13 02:23:26 +0300261 struct omap_dss_device *output)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600262{
263 struct drm_encoder *encoder = NULL;
264 struct omap_encoder *omap_encoder;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600265
266 omap_encoder = kzalloc(sizeof(*omap_encoder), GFP_KERNEL);
Joe Perches78110bb2013-02-11 09:41:29 -0800267 if (!omap_encoder)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600268 goto fail;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600269
Laurent Pinchartd96aaad2018-05-31 23:14:43 +0300270 omap_encoder->output = output;
Rob Clarkf5f94542012-12-04 13:59:12 -0600271
Rob Clarkcd5351f2011-11-12 12:09:40 -0600272 encoder = &omap_encoder->base;
273
274 drm_encoder_init(dev, encoder, &omap_encoder_funcs,
Ville Syrjälä13a3d912015-12-09 16:20:18 +0200275 DRM_MODE_ENCODER_TMDS, NULL);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600276 drm_encoder_helper_add(encoder, &omap_encoder_helper_funcs);
277
Rob Clarkcd5351f2011-11-12 12:09:40 -0600278 return encoder;
279
280fail:
YAMANE Toshiaki582bc282012-11-14 19:31:27 +0900281 if (encoder)
Rob Clark65b0bd02011-12-09 23:26:07 -0600282 omap_encoder_destroy(encoder);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600283
284 return NULL;
285}