blob: 288c59dae56ad9326d3e50699deb8ec0445d071d [file] [log] [blame]
Thomas Gleixnercaab2772019-06-03 07:44:50 +02001// SPDX-License-Identifier: GPL-2.0-only
Rob Clarkcd5351f2011-11-12 12:09:40 -06002/*
Andrew F. Davisbb5cdf82017-12-05 14:29:31 -06003 * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
Rob Clarkcd5351f2011-11-12 12:09:40 -06004 * Author: Rob Clark <rob@ti.com>
Rob Clarkcd5351f2011-11-12 12:09:40 -06005 */
6
Peter Ujfalusieb5bc1f2018-02-12 11:44:39 +02007#include <linux/of.h>
8#include <linux/sort.h>
Laurent Pinchart6e471fa2017-05-06 02:57:12 +03009#include <linux/sys_soc.h>
10
Laurent Pinchart748471a52015-03-05 23:42:39 +020011#include <drm/drm_atomic.h>
Laurent Pinchartcef77d42015-03-05 21:50:00 +020012#include <drm/drm_atomic_helper.h>
Laurent Pinchart2d278f52015-03-05 21:31:37 +020013#include <drm/drm_fb_helper.h>
Daniel Vetterfcd70cd2019-01-17 22:03:34 +010014#include <drm/drm_probe_helper.h>
Laurent Pinchart30b71762018-12-07 23:08:35 +020015#include <drm/drm_panel.h>
Rob Clarkcd5351f2011-11-12 12:09:40 -060016
Andy Gross5c137792012-03-05 10:48:39 -060017#include "omap_dmm_tiler.h"
Laurent Pinchart2d278f52015-03-05 21:31:37 +020018#include "omap_drv.h"
Rob Clarkcd5351f2011-11-12 12:09:40 -060019
20#define DRIVER_NAME MODULE_NAME
21#define DRIVER_DESC "OMAP DRM"
22#define DRIVER_DATE "20110917"
23#define DRIVER_MAJOR 1
24#define DRIVER_MINOR 0
25#define DRIVER_PATCHLEVEL 0
26
Rob Clarkcd5351f2011-11-12 12:09:40 -060027/*
28 * mode config funcs
29 */
30
31/* Notes about mapping DSS and DRM entities:
32 * CRTC: overlay
33 * encoder: manager.. with some extension to allow one primary CRTC
34 * and zero or more video CRTC's to be mapped to one encoder?
35 * connector: dssdev.. manager can be attached/detached from different
36 * devices
37 */
38
Tomi Valkeinen5f741b32015-05-29 16:01:18 +030039static void omap_atomic_wait_for_completion(struct drm_device *dev,
40 struct drm_atomic_state *old_state)
41{
Maarten Lankhorst34d88232017-07-19 16:39:17 +020042 struct drm_crtc_state *new_crtc_state;
Tomi Valkeinen5f741b32015-05-29 16:01:18 +030043 struct drm_crtc *crtc;
44 unsigned int i;
45 int ret;
46
Maarten Lankhorst34d88232017-07-19 16:39:17 +020047 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
48 if (!new_crtc_state->active)
Tomi Valkeinen5f741b32015-05-29 16:01:18 +030049 continue;
50
51 ret = omap_crtc_wait_pending(crtc);
52
53 if (!ret)
54 dev_warn(dev->dev,
55 "atomic complete timeout (pipe %u)!\n", i);
56 }
57}
58
Laurent Pincharta9e6f9f2017-05-09 01:27:10 +030059static void omap_atomic_commit_tail(struct drm_atomic_state *old_state)
Laurent Pinchart748471a52015-03-05 23:42:39 +020060{
Laurent Pincharta9e6f9f2017-05-09 01:27:10 +030061 struct drm_device *dev = old_state->dev;
Laurent Pinchart748471a52015-03-05 23:42:39 +020062 struct omap_drm_private *priv = dev->dev_private;
Laurent Pinchart748471a52015-03-05 23:42:39 +020063
Laurent Pinchart50638ae2018-02-13 14:00:42 +020064 priv->dispc_ops->runtime_get(priv->dispc);
Laurent Pinchart69fb7c82015-05-28 02:09:56 +030065
Laurent Pincharta9e6f9f2017-05-09 01:27:10 +030066 /* Apply the atomic update. */
Laurent Pinchart748471a52015-03-05 23:42:39 +020067 drm_atomic_helper_commit_modeset_disables(dev, old_state);
Jyri Sarha897145d2017-01-27 12:04:55 +020068
Tomi Valkeinenfc5cc9672017-08-23 12:19:02 +030069 if (priv->omaprev != 0x3430) {
70 /* With the current dss dispc implementation we have to enable
71 * the new modeset before we can commit planes. The dispc ovl
72 * configuration relies on the video mode configuration been
73 * written into the HW when the ovl configuration is
74 * calculated.
75 *
76 * This approach is not ideal because after a mode change the
77 * plane update is executed only after the first vblank
78 * interrupt. The dispc implementation should be fixed so that
79 * it is able use uncommitted drm state information.
80 */
81 drm_atomic_helper_commit_modeset_enables(dev, old_state);
82 omap_atomic_wait_for_completion(dev, old_state);
Jyri Sarha897145d2017-01-27 12:04:55 +020083
Tomi Valkeinenfc5cc9672017-08-23 12:19:02 +030084 drm_atomic_helper_commit_planes(dev, old_state, 0);
Laurent Pinchart748471a52015-03-05 23:42:39 +020085
Tomi Valkeinenfc5cc9672017-08-23 12:19:02 +030086 drm_atomic_helper_commit_hw_done(old_state);
87 } else {
88 /*
89 * OMAP3 DSS seems to have issues with the work-around above,
90 * resulting in endless sync losts if a crtc is enabled without
91 * a plane. For now, skip the WA for OMAP3.
92 */
93 drm_atomic_helper_commit_planes(dev, old_state, 0);
94
95 drm_atomic_helper_commit_modeset_enables(dev, old_state);
96
97 drm_atomic_helper_commit_hw_done(old_state);
98 }
Laurent Pincharta9e6f9f2017-05-09 01:27:10 +030099
100 /*
101 * Wait for completion of the page flips to ensure that old buffers
102 * can't be touched by the hardware anymore before cleaning up planes.
103 */
Tomi Valkeinen5f741b32015-05-29 16:01:18 +0300104 omap_atomic_wait_for_completion(dev, old_state);
Laurent Pinchart748471a52015-03-05 23:42:39 +0200105
106 drm_atomic_helper_cleanup_planes(dev, old_state);
107
Laurent Pinchart50638ae2018-02-13 14:00:42 +0200108 priv->dispc_ops->runtime_put(priv->dispc);
Laurent Pinchart748471a52015-03-05 23:42:39 +0200109}
110
Laurent Pincharta9e6f9f2017-05-09 01:27:10 +0300111static const struct drm_mode_config_helper_funcs omap_mode_config_helper_funcs = {
112 .atomic_commit_tail = omap_atomic_commit_tail,
113};
Laurent Pinchart748471a52015-03-05 23:42:39 +0200114
Laurent Pincharte6ecefa2012-05-17 13:27:23 +0200115static const struct drm_mode_config_funcs omap_mode_config_funcs = {
Rob Clarkcd5351f2011-11-12 12:09:40 -0600116 .fb_create = omap_framebuffer_create,
Noralf Trønnesef62d302017-12-05 19:25:01 +0100117 .output_poll_changed = drm_fb_helper_output_poll_changed,
Laurent Pinchartcef77d42015-03-05 21:50:00 +0200118 .atomic_check = drm_atomic_helper_check,
Laurent Pincharta9e6f9f2017-05-09 01:27:10 +0300119 .atomic_commit = drm_atomic_helper_commit,
Rob Clarkcd5351f2011-11-12 12:09:40 -0600120};
121
Laurent Pinchart2ee76792018-03-05 15:02:22 +0200122static void omap_disconnect_pipelines(struct drm_device *ddev)
Archit Tanejacc823bd2014-01-02 14:49:52 +0530123{
Peter Ujfalusi52b9ef22018-02-12 11:44:37 +0200124 struct omap_drm_private *priv = ddev->dev_private;
125 unsigned int i;
Archit Tanejacc823bd2014-01-02 14:49:52 +0530126
Laurent Pinchart2ee76792018-03-05 15:02:22 +0200127 for (i = 0; i < priv->num_pipes; i++) {
Laurent Pinchart511afb42018-03-04 23:42:36 +0200128 struct omap_drm_pipeline *pipe = &priv->pipes[i];
Peter Ujfalusi52b9ef22018-02-12 11:44:37 +0200129
Laurent Pinchart30b71762018-12-07 23:08:35 +0200130 if (pipe->output->panel)
131 drm_panel_detach(pipe->output->panel);
132
Laurent Pinchart511afb42018-03-04 23:42:36 +0200133 omapdss_device_disconnect(NULL, pipe->output);
134
135 omapdss_device_put(pipe->output);
Laurent Pinchart511afb42018-03-04 23:42:36 +0200136 pipe->output = NULL;
Peter Ujfalusi52b9ef22018-02-12 11:44:37 +0200137 }
138
Laurent Pincharte48f9f12018-03-07 00:01:33 +0200139 memset(&priv->channels, 0, sizeof(priv->channels));
140
Laurent Pinchart2ee76792018-03-05 15:02:22 +0200141 priv->num_pipes = 0;
Archit Tanejacc823bd2014-01-02 14:49:52 +0530142}
Archit Taneja0d8f3712013-03-26 19:15:19 +0530143
Laurent Pinchart2ee76792018-03-05 15:02:22 +0200144static int omap_connect_pipelines(struct drm_device *ddev)
Archit Taneja3a01ab22014-01-02 14:49:51 +0530145{
Peter Ujfalusi52b9ef22018-02-12 11:44:37 +0200146 struct omap_drm_private *priv = ddev->dev_private;
Laurent Pinchart511afb42018-03-04 23:42:36 +0200147 struct omap_dss_device *output = NULL;
Peter Ujfalusi52b9ef22018-02-12 11:44:37 +0200148 int r;
Peter Ujfalusia09d2bc2016-05-03 22:08:01 +0300149
Laurent Pinchart511afb42018-03-04 23:42:36 +0200150 for_each_dss_output(output) {
151 r = omapdss_device_connect(priv->dss, NULL, output);
Archit Taneja3a01ab22014-01-02 14:49:51 +0530152 if (r == -EPROBE_DEFER) {
Laurent Pinchart511afb42018-03-04 23:42:36 +0200153 omapdss_device_put(output);
Laurent Pincharta4e26522018-09-23 14:13:15 +0300154 return r;
Archit Taneja3a01ab22014-01-02 14:49:51 +0530155 } else if (r) {
Laurent Pinchart511afb42018-03-04 23:42:36 +0200156 dev_warn(output->dev, "could not connect output %s\n",
157 output->name);
Peter Ujfalusi52b9ef22018-02-12 11:44:37 +0200158 } else {
Laurent Pinchart511afb42018-03-04 23:42:36 +0200159 struct omap_drm_pipeline *pipe;
160
161 pipe = &priv->pipes[priv->num_pipes++];
162 pipe->output = omapdss_device_get(output);
Laurent Pinchart511afb42018-03-04 23:42:36 +0200163
Laurent Pinchart2ee76792018-03-05 15:02:22 +0200164 if (priv->num_pipes == ARRAY_SIZE(priv->pipes)) {
Laurent Pinchart511afb42018-03-04 23:42:36 +0200165 /* To balance the 'for_each_dss_output' loop */
166 omapdss_device_put(output);
Peter Ujfalusi52b9ef22018-02-12 11:44:37 +0200167 break;
168 }
Archit Taneja3a01ab22014-01-02 14:49:51 +0530169 }
170 }
171
Archit Taneja3a01ab22014-01-02 14:49:51 +0530172 return 0;
Laurent Pincharta4e26522018-09-23 14:13:15 +0300173}
Archit Taneja3a01ab22014-01-02 14:49:51 +0530174
Laurent Pincharta4e26522018-09-23 14:13:15 +0300175static int omap_compare_pipelines(const void *a, const void *b)
176{
177 const struct omap_drm_pipeline *pipe1 = a;
178 const struct omap_drm_pipeline *pipe2 = b;
Archit Taneja3a01ab22014-01-02 14:49:51 +0530179
Laurent Pincharta4e26522018-09-23 14:13:15 +0300180 if (pipe1->alias_id > pipe2->alias_id)
181 return 1;
182 else if (pipe1->alias_id < pipe2->alias_id)
183 return -1;
184 return 0;
Archit Taneja3a01ab22014-01-02 14:49:51 +0530185}
Rob Clarkcd5351f2011-11-12 12:09:40 -0600186
Laurent Pincharte2cd09b2015-03-06 17:16:43 +0200187static int omap_modeset_init_properties(struct drm_device *dev)
188{
189 struct omap_drm_private *priv = dev->dev_private;
Laurent Pinchart50638ae2018-02-13 14:00:42 +0200190 unsigned int num_planes = priv->dispc_ops->get_num_ovls(priv->dispc);
Laurent Pincharte2cd09b2015-03-06 17:16:43 +0200191
Laurent Pinchartdff6c242017-05-09 01:27:14 +0300192 priv->zorder_prop = drm_property_create_range(dev, 0, "zorder", 0,
193 num_planes - 1);
Laurent Pincharte2cd09b2015-03-06 17:16:43 +0200194 if (!priv->zorder_prop)
195 return -ENOMEM;
196
197 return 0;
198}
199
Laurent Pinchart79107f22018-09-23 12:58:15 +0300200static int omap_display_id(struct omap_dss_device *output)
201{
202 struct device_node *node = NULL;
203
204 if (output->next) {
205 struct omap_dss_device *display;
206
207 display = omapdss_display_get(output);
208 node = display->dev->of_node;
209 omapdss_device_put(display);
Laurent Pinchart30b71762018-12-07 23:08:35 +0200210 } else if (output->bridge) {
Laurent Pinchart79107f22018-09-23 12:58:15 +0300211 struct drm_bridge *bridge = output->bridge;
212
213 while (bridge->next)
214 bridge = bridge->next;
215
216 node = bridge->of_node;
Laurent Pinchart30b71762018-12-07 23:08:35 +0200217 } else if (output->panel) {
218 node = output->panel->dev->of_node;
Laurent Pinchart79107f22018-09-23 12:58:15 +0300219 }
220
221 return node ? of_alias_get_id(node, "display") : -ENODEV;
222}
223
Rob Clarkcd5351f2011-11-12 12:09:40 -0600224static int omap_modeset_init(struct drm_device *dev)
225{
Rob Clarkcd5351f2011-11-12 12:09:40 -0600226 struct omap_drm_private *priv = dev->dev_private;
Laurent Pinchart50638ae2018-02-13 14:00:42 +0200227 int num_ovls = priv->dispc_ops->get_num_ovls(priv->dispc);
228 int num_mgrs = priv->dispc_ops->get_num_mgrs(priv->dispc);
Peter Ujfalusi52b9ef22018-02-12 11:44:37 +0200229 unsigned int i;
Laurent Pinchartfb9a35f2015-01-11 16:30:44 +0200230 int ret;
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200231 u32 plane_crtc_mask;
Tomi Valkeinen04b1fc02013-05-14 10:55:19 +0300232
Laurent Pincharta4e26522018-09-23 14:13:15 +0300233 if (!omapdss_stack_is_ready())
234 return -EPROBE_DEFER;
235
Rob Clarkcd5351f2011-11-12 12:09:40 -0600236 drm_mode_config_init(dev);
237
Laurent Pincharte2cd09b2015-03-06 17:16:43 +0200238 ret = omap_modeset_init_properties(dev);
239 if (ret < 0)
240 return ret;
241
Rob Clarkf5f94542012-12-04 13:59:12 -0600242 /*
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200243 * This function creates exactly one connector, encoder, crtc,
244 * and primary plane per each connected dss-device. Each
245 * connector->encoder->crtc chain is expected to be separate
246 * and each crtc is connect to a single dss-channel. If the
247 * configuration does not match the expectations or exceeds
248 * the available resources, the configuration is rejected.
Rob Clarkf5f94542012-12-04 13:59:12 -0600249 */
Laurent Pincharta4e26522018-09-23 14:13:15 +0300250 ret = omap_connect_pipelines(dev);
251 if (ret < 0)
252 return ret;
253
Laurent Pinchart2ee76792018-03-05 15:02:22 +0200254 if (priv->num_pipes > num_mgrs || priv->num_pipes > num_ovls) {
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200255 dev_err(dev->dev, "%s(): Too many connected displays\n",
256 __func__);
257 return -EINVAL;
258 }
259
Laurent Pinchartac3b1312018-03-05 19:11:30 +0200260 /* Create all planes first. They can all be put to any CRTC. */
Laurent Pinchart2ee76792018-03-05 15:02:22 +0200261 plane_crtc_mask = (1 << priv->num_pipes) - 1;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600262
Laurent Pinchartac3b1312018-03-05 19:11:30 +0200263 for (i = 0; i < num_ovls; i++) {
Laurent Pinchart2ee76792018-03-05 15:02:22 +0200264 enum drm_plane_type type = i < priv->num_pipes
Laurent Pinchartac3b1312018-03-05 19:11:30 +0200265 ? DRM_PLANE_TYPE_PRIMARY
266 : DRM_PLANE_TYPE_OVERLAY;
267 struct drm_plane *plane;
268
269 if (WARN_ON(priv->num_planes >= ARRAY_SIZE(priv->planes)))
270 return -EINVAL;
271
272 plane = omap_plane_init(dev, i, type, plane_crtc_mask);
273 if (IS_ERR(plane))
274 return PTR_ERR(plane);
275
276 priv->planes[priv->num_planes++] = plane;
277 }
278
Laurent Pinchart79107f22018-09-23 12:58:15 +0300279 /*
280 * Create the encoders, attach the bridges and get the pipeline alias
281 * IDs.
282 */
Laurent Pinchart2ee76792018-03-05 15:02:22 +0200283 for (i = 0; i < priv->num_pipes; i++) {
284 struct omap_drm_pipeline *pipe = &priv->pipes[i];
Laurent Pincharta4e26522018-09-23 14:13:15 +0300285 int id;
286
287 pipe->encoder = omap_encoder_init(dev, pipe->output);
288 if (!pipe->encoder)
289 return -ENOMEM;
290
Laurent Pinchart79107f22018-09-23 12:58:15 +0300291 if (pipe->output->bridge) {
292 ret = drm_bridge_attach(pipe->encoder,
293 pipe->output->bridge, NULL);
294 if (ret < 0)
295 return ret;
296 }
297
298 id = omap_display_id(pipe->output);
Laurent Pincharta4e26522018-09-23 14:13:15 +0300299 pipe->alias_id = id >= 0 ? id : i;
300 }
301
302 /* Sort the pipelines by DT aliases. */
303 sort(priv->pipes, priv->num_pipes, sizeof(priv->pipes[0]),
304 omap_compare_pipelines, NULL);
305
306 /*
307 * Populate the pipeline lookup table by DISPC channel. Only one display
308 * is allowed per channel.
309 */
310 for (i = 0; i < priv->num_pipes; ++i) {
311 struct omap_drm_pipeline *pipe = &priv->pipes[i];
312 enum omap_channel channel = pipe->output->dispc_channel;
313
314 if (WARN_ON(priv->channels[channel] != NULL))
315 return -EINVAL;
316
317 priv->channels[channel] = pipe;
318 }
319
320 /* Create the connectors and CRTCs. */
321 for (i = 0; i < priv->num_pipes; i++) {
322 struct omap_drm_pipeline *pipe = &priv->pipes[i];
323 struct drm_encoder *encoder = pipe->encoder;
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200324 struct drm_crtc *crtc;
Rob Clarkf5f94542012-12-04 13:59:12 -0600325
Laurent Pinchart79107f22018-09-23 12:58:15 +0300326 if (!pipe->output->bridge) {
327 pipe->connector = omap_connector_init(dev, pipe->output,
328 encoder);
329 if (!pipe->connector)
330 return -ENOMEM;
Rob Clarkf5f94542012-12-04 13:59:12 -0600331
Laurent Pinchart79107f22018-09-23 12:58:15 +0300332 drm_connector_attach_encoder(pipe->connector, encoder);
Laurent Pinchart30b71762018-12-07 23:08:35 +0200333
334 if (pipe->output->panel) {
335 ret = drm_panel_attach(pipe->output->panel,
336 pipe->connector);
337 if (ret < 0)
338 return ret;
339 }
Laurent Pinchart79107f22018-09-23 12:58:15 +0300340 }
Laurent Pincharta4e26522018-09-23 14:13:15 +0300341
Laurent Pinchart00b30e72018-03-06 23:37:25 +0200342 crtc = omap_crtc_init(dev, pipe, priv->planes[i]);
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200343 if (IS_ERR(crtc))
344 return PTR_ERR(crtc);
345
Laurent Pinchartf9699362018-03-05 14:47:47 +0200346 encoder->possible_crtcs = 1 << i;
Laurent Pinchart2ee76792018-03-05 15:02:22 +0200347 pipe->crtc = crtc;
Archit Taneja0d8f3712013-03-26 19:15:19 +0530348 }
349
Laurent Pinchart2ee76792018-03-05 15:02:22 +0200350 DBG("registered %u planes, %u crtcs/encoders/connectors\n",
351 priv->num_planes, priv->num_pipes);
Archit Taneja0d8f3712013-03-26 19:15:19 +0530352
Tomi Valkeinen1e907112016-08-23 12:35:39 +0300353 dev->mode_config.min_width = 8;
354 dev->mode_config.min_height = 2;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600355
Tomi Valkeinen1915d7f2018-01-10 11:31:18 +0200356 /*
357 * Note: these values are used for multiple independent things:
358 * connector mode filtering, buffer sizes, crtc sizes...
359 * Use big enough values here to cover all use cases, and do more
360 * specific checking in the respective code paths.
Rob Clarkcd5351f2011-11-12 12:09:40 -0600361 */
Tomi Valkeinen1915d7f2018-01-10 11:31:18 +0200362 dev->mode_config.max_width = 8192;
363 dev->mode_config.max_height = 8192;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600364
Peter Ujfalusi23936ba2018-03-21 12:20:29 +0200365 /* We want the zpos to be normalized */
366 dev->mode_config.normalize_zpos = true;
367
Rob Clarkcd5351f2011-11-12 12:09:40 -0600368 dev->mode_config.funcs = &omap_mode_config_funcs;
Laurent Pincharta9e6f9f2017-05-09 01:27:10 +0300369 dev->mode_config.helper_private = &omap_mode_config_helper_funcs;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600370
Laurent Pinchart69a12262015-03-05 21:38:16 +0200371 drm_mode_config_reset(dev);
372
Laurent Pinchart728ae8d2015-05-28 00:21:29 +0300373 omap_drm_irq_install(dev);
374
Rob Clarkcd5351f2011-11-12 12:09:40 -0600375 return 0;
376}
377
Rob Clarkcd5351f2011-11-12 12:09:40 -0600378/*
Peter Ujfalusi3c596802017-06-02 15:26:35 +0300379 * Enable the HPD in external components if supported
380 */
Peter Ujfalusi52b9ef22018-02-12 11:44:37 +0200381static void omap_modeset_enable_external_hpd(struct drm_device *ddev)
Peter Ujfalusi3c596802017-06-02 15:26:35 +0300382{
Peter Ujfalusi52b9ef22018-02-12 11:44:37 +0200383 struct omap_drm_private *priv = ddev->dev_private;
Laurent Pinchart79107f22018-09-23 12:58:15 +0300384 unsigned int i;
Peter Ujfalusi3c596802017-06-02 15:26:35 +0300385
Laurent Pinchart79107f22018-09-23 12:58:15 +0300386 for (i = 0; i < priv->num_pipes; i++) {
387 if (priv->pipes[i].connector)
388 omap_connector_enable_hpd(priv->pipes[i].connector);
389 }
Peter Ujfalusi3c596802017-06-02 15:26:35 +0300390}
391
392/*
393 * Disable the HPD in external components if supported
394 */
Peter Ujfalusi52b9ef22018-02-12 11:44:37 +0200395static void omap_modeset_disable_external_hpd(struct drm_device *ddev)
Peter Ujfalusi3c596802017-06-02 15:26:35 +0300396{
Peter Ujfalusi52b9ef22018-02-12 11:44:37 +0200397 struct omap_drm_private *priv = ddev->dev_private;
Laurent Pinchart79107f22018-09-23 12:58:15 +0300398 unsigned int i;
Peter Ujfalusi3c596802017-06-02 15:26:35 +0300399
Laurent Pinchart79107f22018-09-23 12:58:15 +0300400 for (i = 0; i < priv->num_pipes; i++) {
401 if (priv->pipes[i].connector)
402 omap_connector_disable_hpd(priv->pipes[i].connector);
403 }
Peter Ujfalusi3c596802017-06-02 15:26:35 +0300404}
405
406/*
Rob Clarkcd5351f2011-11-12 12:09:40 -0600407 * drm ioctl funcs
408 */
409
410
411static int ioctl_get_param(struct drm_device *dev, void *data,
412 struct drm_file *file_priv)
413{
Rob Clark5e3b0872012-10-29 09:31:12 +0100414 struct omap_drm_private *priv = dev->dev_private;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600415 struct drm_omap_param *args = data;
416
417 DBG("%p: param=%llu", dev, args->param);
418
419 switch (args->param) {
420 case OMAP_PARAM_CHIPSET_ID:
Rob Clark5e3b0872012-10-29 09:31:12 +0100421 args->value = priv->omaprev;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600422 break;
423 default:
424 DBG("unknown parameter %lld", args->param);
425 return -EINVAL;
426 }
427
428 return 0;
429}
430
Laurent Pinchartef3f4e92015-12-14 22:39:36 +0200431#define OMAP_BO_USER_MASK 0x00ffffff /* flags settable by userspace */
432
Rob Clarkcd5351f2011-11-12 12:09:40 -0600433static int ioctl_gem_new(struct drm_device *dev, void *data,
434 struct drm_file *file_priv)
435{
436 struct drm_omap_gem_new *args = data;
Laurent Pinchartef3f4e92015-12-14 22:39:36 +0200437 u32 flags = args->flags & OMAP_BO_USER_MASK;
438
Rob Clarkf5f94542012-12-04 13:59:12 -0600439 VERB("%p:%p: size=0x%08x, flags=%08x", dev, file_priv,
Laurent Pinchartef3f4e92015-12-14 22:39:36 +0200440 args->size.bytes, flags);
441
442 return omap_gem_new_handle(dev, file_priv, args->size, flags,
443 &args->handle);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600444}
445
Rob Clarkcd5351f2011-11-12 12:09:40 -0600446static int ioctl_gem_info(struct drm_device *dev, void *data,
447 struct drm_file *file_priv)
448{
449 struct drm_omap_gem_info *args = data;
450 struct drm_gem_object *obj;
451 int ret = 0;
452
Rob Clarkf5f94542012-12-04 13:59:12 -0600453 VERB("%p:%p: handle=%d", dev, file_priv, args->handle);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600454
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100455 obj = drm_gem_object_lookup(file_priv, args->handle);
YAMANE Toshiakic7f904b2012-11-14 19:30:38 +0900456 if (!obj)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600457 return -ENOENT;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600458
Rob Clarkf7f9f452011-12-05 19:19:22 -0600459 args->size = omap_gem_mmap_size(obj);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600460 args->offset = omap_gem_mmap_offset(obj);
461
Thomas Zimmermanne64d0222018-06-18 15:07:26 +0200462 drm_gem_object_put_unlocked(obj);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600463
464 return ret;
465}
466
Rob Clarkbaa70942013-08-02 13:27:49 -0400467static const struct drm_ioctl_desc ioctls[DRM_COMMAND_END - DRM_COMMAND_BASE] = {
Hemant Hariyani5f6ab8c2016-06-07 13:23:19 -0500468 DRM_IOCTL_DEF_DRV(OMAP_GET_PARAM, ioctl_get_param,
469 DRM_AUTH | DRM_RENDER_ALLOW),
Emil Velikov9a671c22019-05-22 16:02:18 +0100470 DRM_IOCTL_DEF_DRV(OMAP_SET_PARAM, drm_invalid_op,
Hemant Hariyani5f6ab8c2016-06-07 13:23:19 -0500471 DRM_AUTH | DRM_MASTER | DRM_ROOT_ONLY),
472 DRM_IOCTL_DEF_DRV(OMAP_GEM_NEW, ioctl_gem_new,
473 DRM_AUTH | DRM_RENDER_ALLOW),
Laurent Pinchartd6f544f2017-05-09 01:27:11 +0300474 /* Deprecated, to be removed. */
475 DRM_IOCTL_DEF_DRV(OMAP_GEM_CPU_PREP, drm_noop,
Hemant Hariyani5f6ab8c2016-06-07 13:23:19 -0500476 DRM_AUTH | DRM_RENDER_ALLOW),
Laurent Pinchartd6f544f2017-05-09 01:27:11 +0300477 /* Deprecated, to be removed. */
478 DRM_IOCTL_DEF_DRV(OMAP_GEM_CPU_FINI, drm_noop,
Hemant Hariyani5f6ab8c2016-06-07 13:23:19 -0500479 DRM_AUTH | DRM_RENDER_ALLOW),
480 DRM_IOCTL_DEF_DRV(OMAP_GEM_INFO, ioctl_gem_info,
481 DRM_AUTH | DRM_RENDER_ALLOW),
Rob Clarkcd5351f2011-11-12 12:09:40 -0600482};
483
484/*
485 * drm driver funcs
486 */
487
Rob Clarkcd5351f2011-11-12 12:09:40 -0600488static int dev_open(struct drm_device *dev, struct drm_file *file)
489{
490 file->driver_priv = NULL;
491
492 DBG("open: dev=%p, file=%p", dev, file);
493
494 return 0;
495}
496
Laurent Pinchart78b68552012-05-17 13:27:22 +0200497static const struct vm_operations_struct omap_gem_vm_ops = {
Rob Clarkcd5351f2011-11-12 12:09:40 -0600498 .fault = omap_gem_fault,
499 .open = drm_gem_vm_open,
500 .close = drm_gem_vm_close,
501};
502
Rob Clarkff4f3872012-01-16 12:51:14 -0600503static const struct file_operations omapdriver_fops = {
Laurent Pinchart222025e2015-01-11 00:02:07 +0200504 .owner = THIS_MODULE,
505 .open = drm_open,
506 .unlocked_ioctl = drm_ioctl,
Tomi Valkeinen9d24159a2017-02-24 13:24:50 +0200507 .compat_ioctl = drm_compat_ioctl,
Laurent Pinchart222025e2015-01-11 00:02:07 +0200508 .release = drm_release,
509 .mmap = omap_gem_mmap,
510 .poll = drm_poll,
511 .read = drm_read,
512 .llseek = noop_llseek,
Rob Clarkff4f3872012-01-16 12:51:14 -0600513};
514
Rob Clarkcd5351f2011-11-12 12:09:40 -0600515static struct drm_driver omap_drm_driver = {
Tomi Valkeinen728fea72015-10-02 11:10:41 +0300516 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME |
Hemant Hariyani5f6ab8c2016-06-07 13:23:19 -0500517 DRIVER_ATOMIC | DRIVER_RENDER,
Laurent Pinchart222025e2015-01-11 00:02:07 +0200518 .open = dev_open,
Noralf Trønnesef62d302017-12-05 19:25:01 +0100519 .lastclose = drm_fb_helper_lastclose,
Andy Gross6169a1482011-12-15 21:05:17 -0600520#ifdef CONFIG_DEBUG_FS
Laurent Pinchart222025e2015-01-11 00:02:07 +0200521 .debugfs_init = omap_debugfs_init,
Andy Gross6169a1482011-12-15 21:05:17 -0600522#endif
Laurent Pinchart222025e2015-01-11 00:02:07 +0200523 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
524 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
525 .gem_prime_export = omap_gem_prime_export,
526 .gem_prime_import = omap_gem_prime_import,
Daniel Vetterf8466182018-05-25 19:39:25 +0300527 .gem_free_object_unlocked = omap_gem_free_object,
Laurent Pinchart222025e2015-01-11 00:02:07 +0200528 .gem_vm_ops = &omap_gem_vm_ops,
529 .dumb_create = omap_gem_dumb_create,
530 .dumb_map_offset = omap_gem_dumb_map_offset,
Laurent Pinchart222025e2015-01-11 00:02:07 +0200531 .ioctls = ioctls,
532 .num_ioctls = DRM_OMAP_NUM_IOCTLS,
533 .fops = &omapdriver_fops,
534 .name = DRIVER_NAME,
535 .desc = DRIVER_DESC,
536 .date = DRIVER_DATE,
537 .major = DRIVER_MAJOR,
538 .minor = DRIVER_MINOR,
539 .patchlevel = DRIVER_PATCHLEVEL,
Rob Clarkcd5351f2011-11-12 12:09:40 -0600540};
541
Laurent Pinchart6e471fa2017-05-06 02:57:12 +0300542static const struct soc_device_attribute omapdrm_soc_devices[] = {
543 { .family = "OMAP3", .data = (void *)0x3430 },
544 { .family = "OMAP4", .data = (void *)0x4430 },
545 { .family = "OMAP5", .data = (void *)0x5430 },
546 { .family = "DRA7", .data = (void *)0x0752 },
547 { /* sentinel */ }
548};
549
Laurent Pincharta82f03472018-02-13 14:00:19 +0200550static int omapdrm_init(struct omap_drm_private *priv, struct device *dev)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600551{
Laurent Pinchart6e471fa2017-05-06 02:57:12 +0300552 const struct soc_device_attribute *soc;
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200553 struct drm_device *ddev;
554 unsigned int i;
555 int ret;
556
Laurent Pincharta82f03472018-02-13 14:00:19 +0200557 DBG("%s", dev_name(dev));
Archit Taneja3a01ab22014-01-02 14:49:51 +0530558
Peter Ujfalusifb96b672018-02-12 11:44:36 +0200559 /* Allocate and initialize the DRM device. */
560 ddev = drm_dev_alloc(&omap_drm_driver, dev);
561 if (IS_ERR(ddev))
562 return PTR_ERR(ddev);
563
564 priv->ddev = ddev;
565 ddev->dev_private = priv;
566
Laurent Pincharta82f03472018-02-13 14:00:19 +0200567 priv->dev = dev;
Laurent Pinchartd3541ca2018-02-13 14:00:41 +0200568 priv->dss = omapdss_get_dss();
Laurent Pinchart50638ae2018-02-13 14:00:42 +0200569 priv->dispc = dispc_get_dispc(priv->dss);
Laurent Pinchartd3541ca2018-02-13 14:00:41 +0200570 priv->dispc_ops = dispc_get_ops(priv->dss);
Laurent Pinchart510c74c2017-08-11 16:49:08 +0300571
Laurent Pinchart64cb8172018-02-13 14:00:39 +0200572 omap_crtc_pre_init(priv);
Archit Taneja3a01ab22014-01-02 14:49:51 +0530573
Laurent Pinchart6e471fa2017-05-06 02:57:12 +0300574 soc = soc_device_match(omapdrm_soc_devices);
575 priv->omaprev = soc ? (unsigned int)soc->data : 0;
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200576 priv->wq = alloc_ordered_workqueue("omapdrm", 0);
577
Daniel Vetter5117bd82018-05-25 19:39:24 +0300578 mutex_init(&priv->list_lock);
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200579 INIT_LIST_HEAD(&priv->obj_list);
580
Peter Ujfalusia7631c42017-11-30 14:12:37 +0200581 /* Get memory bandwidth limits */
582 if (priv->dispc_ops->get_memory_bandwidth_limit)
583 priv->max_bandwidth =
Laurent Pinchart50638ae2018-02-13 14:00:42 +0200584 priv->dispc_ops->get_memory_bandwidth_limit(priv->dispc);
Peter Ujfalusia7631c42017-11-30 14:12:37 +0200585
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200586 omap_gem_init(ddev);
587
588 ret = omap_modeset_init(ddev);
589 if (ret) {
Laurent Pincharta82f03472018-02-13 14:00:19 +0200590 dev_err(priv->dev, "omap_modeset_init failed: ret=%d\n", ret);
Peter Ujfalusifb96b672018-02-12 11:44:36 +0200591 goto err_gem_deinit;
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200592 }
593
594 /* Initialize vblank handling, start with all CRTCs disabled. */
Laurent Pinchart2ee76792018-03-05 15:02:22 +0200595 ret = drm_vblank_init(ddev, priv->num_pipes);
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200596 if (ret) {
Laurent Pincharta82f03472018-02-13 14:00:19 +0200597 dev_err(priv->dev, "could not init vblank\n");
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200598 goto err_cleanup_modeset;
599 }
600
Laurent Pinchart2ee76792018-03-05 15:02:22 +0200601 for (i = 0; i < priv->num_pipes; i++)
602 drm_crtc_vblank_off(priv->pipes[i].crtc);
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200603
Tomi Valkeinenefd1f062018-02-09 09:36:23 +0200604 omap_fbdev_init(ddev);
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200605
606 drm_kms_helper_poll_init(ddev);
Peter Ujfalusi52b9ef22018-02-12 11:44:37 +0200607 omap_modeset_enable_external_hpd(ddev);
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200608
609 /*
610 * Register the DRM device with the core and the connectors with
611 * sysfs.
612 */
613 ret = drm_dev_register(ddev, 0);
614 if (ret)
615 goto err_cleanup_helpers;
616
617 return 0;
618
619err_cleanup_helpers:
Peter Ujfalusi52b9ef22018-02-12 11:44:37 +0200620 omap_modeset_disable_external_hpd(ddev);
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200621 drm_kms_helper_poll_fini(ddev);
Tomi Valkeinenefd1f062018-02-09 09:36:23 +0200622
623 omap_fbdev_fini(ddev);
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200624err_cleanup_modeset:
625 drm_mode_config_cleanup(ddev);
626 omap_drm_irq_uninstall(ddev);
Peter Ujfalusifb96b672018-02-12 11:44:36 +0200627err_gem_deinit:
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200628 omap_gem_deinit(ddev);
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200629 destroy_workqueue(priv->wq);
Laurent Pinchart2ee76792018-03-05 15:02:22 +0200630 omap_disconnect_pipelines(ddev);
Laurent Pinchart845417b2018-03-02 03:05:10 +0200631 omap_crtc_pre_uninit(priv);
Thomas Zimmermann08bafff2018-06-18 15:07:27 +0200632 drm_dev_put(ddev);
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200633 return ret;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600634}
635
Laurent Pincharta82f03472018-02-13 14:00:19 +0200636static void omapdrm_cleanup(struct omap_drm_private *priv)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600637{
Laurent Pincharta82f03472018-02-13 14:00:19 +0200638 struct drm_device *ddev = priv->ddev;
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200639
Rob Clarkcd5351f2011-11-12 12:09:40 -0600640 DBG("");
Andy Gross5c137792012-03-05 10:48:39 -0600641
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200642 drm_dev_unregister(ddev);
643
Peter Ujfalusi52b9ef22018-02-12 11:44:37 +0200644 omap_modeset_disable_external_hpd(ddev);
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200645 drm_kms_helper_poll_fini(ddev);
646
Tomi Valkeinenefd1f062018-02-09 09:36:23 +0200647 omap_fbdev_fini(ddev);
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200648
Tomi Valkeinen8a54aa92017-03-27 10:02:22 +0300649 drm_atomic_helper_shutdown(ddev);
650
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200651 drm_mode_config_cleanup(ddev);
652
653 omap_drm_irq_uninstall(ddev);
654 omap_gem_deinit(ddev);
655
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200656 destroy_workqueue(priv->wq);
Tomi Valkeinen707cf582014-04-02 13:47:43 +0300657
Laurent Pinchart2ee76792018-03-05 15:02:22 +0200658 omap_disconnect_pipelines(ddev);
Laurent Pinchart845417b2018-03-02 03:05:10 +0200659 omap_crtc_pre_uninit(priv);
Peter Ujfalusifb96b672018-02-12 11:44:36 +0200660
Thomas Zimmermann08bafff2018-06-18 15:07:27 +0200661 drm_dev_put(ddev);
Laurent Pincharta82f03472018-02-13 14:00:19 +0200662}
663
664static int pdev_probe(struct platform_device *pdev)
665{
666 struct omap_drm_private *priv;
667 int ret;
668
669 if (omapdss_is_initialized() == false)
670 return -EPROBE_DEFER;
671
672 ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
673 if (ret) {
674 dev_err(&pdev->dev, "Failed to set the DMA mask\n");
675 return ret;
676 }
677
678 /* Allocate and initialize the driver private structure. */
679 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
680 if (!priv)
681 return -ENOMEM;
682
683 platform_set_drvdata(pdev, priv);
684
685 ret = omapdrm_init(priv, &pdev->dev);
686 if (ret < 0)
687 kfree(priv);
688
689 return ret;
690}
691
692static int pdev_remove(struct platform_device *pdev)
693{
694 struct omap_drm_private *priv = platform_get_drvdata(pdev);
695
696 omapdrm_cleanup(priv);
697 kfree(priv);
Daniel Vetterfd3c0252013-12-11 11:34:26 +0100698
Rob Clarkcd5351f2011-11-12 12:09:40 -0600699 return 0;
700}
701
Grygorii Strashko8450c8d2015-02-26 15:57:17 +0200702#ifdef CONFIG_PM_SLEEP
Tomi Valkeinenccd7b5e2014-11-14 15:18:28 +0200703static int omap_drm_suspend(struct device *dev)
704{
Laurent Pincharta82f03472018-02-13 14:00:19 +0200705 struct omap_drm_private *priv = dev_get_drvdata(dev);
706 struct drm_device *drm_dev = priv->ddev;
Tomi Valkeinenccd7b5e2014-11-14 15:18:28 +0200707
Laurent Pinchartd2c53162018-09-04 17:08:33 +0300708 return drm_mode_config_helper_suspend(drm_dev);
Tomi Valkeinenccd7b5e2014-11-14 15:18:28 +0200709}
710
711static int omap_drm_resume(struct device *dev)
712{
Laurent Pincharta82f03472018-02-13 14:00:19 +0200713 struct omap_drm_private *priv = dev_get_drvdata(dev);
714 struct drm_device *drm_dev = priv->ddev;
Tomi Valkeinenccd7b5e2014-11-14 15:18:28 +0200715
Laurent Pinchartd2c53162018-09-04 17:08:33 +0300716 drm_mode_config_helper_resume(drm_dev);
Tomi Valkeinenccd7b5e2014-11-14 15:18:28 +0200717
Laurent Pinchart7fb15c42017-10-13 17:58:58 +0300718 return omap_gem_resume(drm_dev);
Tomi Valkeinenccd7b5e2014-11-14 15:18:28 +0200719}
Andy Grosse78edba2012-12-19 14:53:37 -0600720#endif
721
Grygorii Strashko8450c8d2015-02-26 15:57:17 +0200722static SIMPLE_DEV_PM_OPS(omapdrm_pm_ops, omap_drm_suspend, omap_drm_resume);
723
Tomi Valkeinen6717cd22013-04-10 10:44:00 +0300724static struct platform_driver pdev = {
Laurent Pinchart222025e2015-01-11 00:02:07 +0200725 .driver = {
Tomi Valkeinenf64eafa2017-08-16 12:43:55 +0300726 .name = "omapdrm",
Laurent Pinchart222025e2015-01-11 00:02:07 +0200727 .pm = &omapdrm_pm_ops,
Laurent Pinchart222025e2015-01-11 00:02:07 +0200728 },
729 .probe = pdev_probe,
730 .remove = pdev_remove,
Rob Clarkcd5351f2011-11-12 12:09:40 -0600731};
732
Thierry Redinge1c49bd2015-12-02 17:23:31 +0100733static struct platform_driver * const drivers[] = {
734 &omap_dmm_driver,
735 &pdev,
736};
737
Rob Clarkcd5351f2011-11-12 12:09:40 -0600738static int __init omap_drm_init(void)
739{
740 DBG("init");
Tomi Valkeinenea7e3a62014-04-02 14:31:50 +0300741
Thierry Redinge1c49bd2015-12-02 17:23:31 +0100742 return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
Rob Clarkcd5351f2011-11-12 12:09:40 -0600743}
744
745static void __exit omap_drm_fini(void)
746{
747 DBG("fini");
Tomi Valkeinenea7e3a62014-04-02 14:31:50 +0300748
Thierry Redinge1c49bd2015-12-02 17:23:31 +0100749 platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
Rob Clarkcd5351f2011-11-12 12:09:40 -0600750}
751
752/* need late_initcall() so we load after dss_driver's are loaded */
753late_initcall(omap_drm_init);
754module_exit(omap_drm_fini);
755
756MODULE_AUTHOR("Rob Clark <rob@ti.com>");
757MODULE_DESCRIPTION("OMAP DRM Display Driver");
758MODULE_ALIAS("platform:" DRIVER_NAME);
759MODULE_LICENSE("GPL v2");