blob: cdf5b0601ebabee12cb876c2d632bc59b18ad66d [file] [log] [blame]
Rob Clarkcd5351f2011-11-12 12:09:40 -06001/*
Rob Clark8bb0daf2013-02-11 12:43:09 -05002 * drivers/gpu/drm/omapdrm/omap_drv.c
Rob Clarkcd5351f2011-11-12 12:09:40 -06003 *
4 * Copyright (C) 2011 Texas Instruments
5 * Author: Rob Clark <rob@ti.com>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 as published by
9 * the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
Laurent Pinchart6e471fa2017-05-06 02:57:12 +030020#include <linux/sys_soc.h>
21
Laurent Pinchart748471a52015-03-05 23:42:39 +020022#include <drm/drm_atomic.h>
Laurent Pinchartcef77d42015-03-05 21:50:00 +020023#include <drm/drm_atomic_helper.h>
Laurent Pinchart2d278f52015-03-05 21:31:37 +020024#include <drm/drm_crtc_helper.h>
25#include <drm/drm_fb_helper.h>
Rob Clarkcd5351f2011-11-12 12:09:40 -060026
Andy Gross5c137792012-03-05 10:48:39 -060027#include "omap_dmm_tiler.h"
Laurent Pinchart2d278f52015-03-05 21:31:37 +020028#include "omap_drv.h"
Rob Clarkcd5351f2011-11-12 12:09:40 -060029
30#define DRIVER_NAME MODULE_NAME
31#define DRIVER_DESC "OMAP DRM"
32#define DRIVER_DATE "20110917"
33#define DRIVER_MAJOR 1
34#define DRIVER_MINOR 0
35#define DRIVER_PATCHLEVEL 0
36
Rob Clarkcd5351f2011-11-12 12:09:40 -060037/*
38 * mode config funcs
39 */
40
41/* Notes about mapping DSS and DRM entities:
42 * CRTC: overlay
43 * encoder: manager.. with some extension to allow one primary CRTC
44 * and zero or more video CRTC's to be mapped to one encoder?
45 * connector: dssdev.. manager can be attached/detached from different
46 * devices
47 */
48
49static void omap_fb_output_poll_changed(struct drm_device *dev)
50{
51 struct omap_drm_private *priv = dev->dev_private;
52 DBG("dev=%p", dev);
YAMANE Toshiakic7f904b2012-11-14 19:30:38 +090053 if (priv->fbdev)
Rob Clarkcd5351f2011-11-12 12:09:40 -060054 drm_fb_helper_hotplug_event(priv->fbdev);
Rob Clarkcd5351f2011-11-12 12:09:40 -060055}
56
Tomi Valkeinen5f741b32015-05-29 16:01:18 +030057static void omap_atomic_wait_for_completion(struct drm_device *dev,
58 struct drm_atomic_state *old_state)
59{
Maarten Lankhorst34d88232017-07-19 16:39:17 +020060 struct drm_crtc_state *new_crtc_state;
Tomi Valkeinen5f741b32015-05-29 16:01:18 +030061 struct drm_crtc *crtc;
62 unsigned int i;
63 int ret;
64
Maarten Lankhorst34d88232017-07-19 16:39:17 +020065 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
66 if (!new_crtc_state->active)
Tomi Valkeinen5f741b32015-05-29 16:01:18 +030067 continue;
68
69 ret = omap_crtc_wait_pending(crtc);
70
71 if (!ret)
72 dev_warn(dev->dev,
73 "atomic complete timeout (pipe %u)!\n", i);
74 }
75}
76
Laurent Pincharta9e6f9f2017-05-09 01:27:10 +030077static void omap_atomic_commit_tail(struct drm_atomic_state *old_state)
Laurent Pinchart748471a52015-03-05 23:42:39 +020078{
Laurent Pincharta9e6f9f2017-05-09 01:27:10 +030079 struct drm_device *dev = old_state->dev;
Laurent Pinchart748471a52015-03-05 23:42:39 +020080 struct omap_drm_private *priv = dev->dev_private;
Laurent Pinchart748471a52015-03-05 23:42:39 +020081
Tomi Valkeinen9f759222015-11-05 18:39:52 +020082 priv->dispc_ops->runtime_get();
Laurent Pinchart69fb7c82015-05-28 02:09:56 +030083
Laurent Pincharta9e6f9f2017-05-09 01:27:10 +030084 /* Apply the atomic update. */
Laurent Pinchart748471a52015-03-05 23:42:39 +020085 drm_atomic_helper_commit_modeset_disables(dev, old_state);
Jyri Sarha897145d2017-01-27 12:04:55 +020086
Tomi Valkeinenfc5cc9672017-08-23 12:19:02 +030087 if (priv->omaprev != 0x3430) {
88 /* With the current dss dispc implementation we have to enable
89 * the new modeset before we can commit planes. The dispc ovl
90 * configuration relies on the video mode configuration been
91 * written into the HW when the ovl configuration is
92 * calculated.
93 *
94 * This approach is not ideal because after a mode change the
95 * plane update is executed only after the first vblank
96 * interrupt. The dispc implementation should be fixed so that
97 * it is able use uncommitted drm state information.
98 */
99 drm_atomic_helper_commit_modeset_enables(dev, old_state);
100 omap_atomic_wait_for_completion(dev, old_state);
Jyri Sarha897145d2017-01-27 12:04:55 +0200101
Tomi Valkeinenfc5cc9672017-08-23 12:19:02 +0300102 drm_atomic_helper_commit_planes(dev, old_state, 0);
Laurent Pinchart748471a52015-03-05 23:42:39 +0200103
Tomi Valkeinenfc5cc9672017-08-23 12:19:02 +0300104 drm_atomic_helper_commit_hw_done(old_state);
105 } else {
106 /*
107 * OMAP3 DSS seems to have issues with the work-around above,
108 * resulting in endless sync losts if a crtc is enabled without
109 * a plane. For now, skip the WA for OMAP3.
110 */
111 drm_atomic_helper_commit_planes(dev, old_state, 0);
112
113 drm_atomic_helper_commit_modeset_enables(dev, old_state);
114
115 drm_atomic_helper_commit_hw_done(old_state);
116 }
Laurent Pincharta9e6f9f2017-05-09 01:27:10 +0300117
118 /*
119 * Wait for completion of the page flips to ensure that old buffers
120 * can't be touched by the hardware anymore before cleaning up planes.
121 */
Tomi Valkeinen5f741b32015-05-29 16:01:18 +0300122 omap_atomic_wait_for_completion(dev, old_state);
Laurent Pinchart748471a52015-03-05 23:42:39 +0200123
124 drm_atomic_helper_cleanup_planes(dev, old_state);
125
Tomi Valkeinen9f759222015-11-05 18:39:52 +0200126 priv->dispc_ops->runtime_put();
Laurent Pinchart748471a52015-03-05 23:42:39 +0200127}
128
Laurent Pincharta9e6f9f2017-05-09 01:27:10 +0300129static const struct drm_mode_config_helper_funcs omap_mode_config_helper_funcs = {
130 .atomic_commit_tail = omap_atomic_commit_tail,
131};
Laurent Pinchart748471a52015-03-05 23:42:39 +0200132
Laurent Pincharte6ecefa2012-05-17 13:27:23 +0200133static const struct drm_mode_config_funcs omap_mode_config_funcs = {
Rob Clarkcd5351f2011-11-12 12:09:40 -0600134 .fb_create = omap_framebuffer_create,
135 .output_poll_changed = omap_fb_output_poll_changed,
Laurent Pinchartcef77d42015-03-05 21:50:00 +0200136 .atomic_check = drm_atomic_helper_check,
Laurent Pincharta9e6f9f2017-05-09 01:27:10 +0300137 .atomic_commit = drm_atomic_helper_commit,
Rob Clarkcd5351f2011-11-12 12:09:40 -0600138};
139
140static int get_connector_type(struct omap_dss_device *dssdev)
141{
142 switch (dssdev->type) {
143 case OMAP_DISPLAY_TYPE_HDMI:
144 return DRM_MODE_CONNECTOR_HDMIA;
Tomi Valkeinen4635c172013-05-14 14:14:15 +0300145 case OMAP_DISPLAY_TYPE_DVI:
146 return DRM_MODE_CONNECTOR_DVID;
Sebastian Reichel4a64b902016-03-08 17:39:36 +0100147 case OMAP_DISPLAY_TYPE_DSI:
148 return DRM_MODE_CONNECTOR_DSI;
Tomi Valkeinen564f88c2017-04-27 13:02:28 +0300149 case OMAP_DISPLAY_TYPE_DPI:
150 case OMAP_DISPLAY_TYPE_DBI:
151 return DRM_MODE_CONNECTOR_DPI;
152 case OMAP_DISPLAY_TYPE_VENC:
153 /* TODO: This could also be composite */
154 return DRM_MODE_CONNECTOR_SVIDEO;
155 case OMAP_DISPLAY_TYPE_SDI:
156 return DRM_MODE_CONNECTOR_LVDS;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600157 default:
158 return DRM_MODE_CONNECTOR_Unknown;
159 }
160}
161
Archit Tanejacc823bd2014-01-02 14:49:52 +0530162static void omap_disconnect_dssdevs(void)
163{
164 struct omap_dss_device *dssdev = NULL;
165
166 for_each_dss_dev(dssdev)
167 dssdev->driver->disconnect(dssdev);
168}
Archit Taneja0d8f3712013-03-26 19:15:19 +0530169
Archit Taneja3a01ab22014-01-02 14:49:51 +0530170static int omap_connect_dssdevs(void)
171{
172 int r;
173 struct omap_dss_device *dssdev = NULL;
Peter Ujfalusia09d2bc2016-05-03 22:08:01 +0300174
175 if (!omapdss_stack_is_ready())
176 return -EPROBE_DEFER;
Archit Taneja3a01ab22014-01-02 14:49:51 +0530177
178 for_each_dss_dev(dssdev) {
179 r = dssdev->driver->connect(dssdev);
180 if (r == -EPROBE_DEFER) {
181 omap_dss_put_device(dssdev);
182 goto cleanup;
183 } else if (r) {
184 dev_warn(dssdev->dev, "could not connect display: %s\n",
185 dssdev->name);
Archit Taneja3a01ab22014-01-02 14:49:51 +0530186 }
187 }
188
Archit Taneja3a01ab22014-01-02 14:49:51 +0530189 return 0;
190
191cleanup:
192 /*
193 * if we are deferring probe, we disconnect the devices we previously
194 * connected
195 */
Archit Tanejacc823bd2014-01-02 14:49:52 +0530196 omap_disconnect_dssdevs();
Archit Taneja3a01ab22014-01-02 14:49:51 +0530197
198 return r;
199}
Rob Clarkcd5351f2011-11-12 12:09:40 -0600200
Laurent Pincharte2cd09b2015-03-06 17:16:43 +0200201static int omap_modeset_init_properties(struct drm_device *dev)
202{
203 struct omap_drm_private *priv = dev->dev_private;
Laurent Pinchartdff6c242017-05-09 01:27:14 +0300204 unsigned int num_planes = priv->dispc_ops->get_num_ovls();
Laurent Pincharte2cd09b2015-03-06 17:16:43 +0200205
Laurent Pinchartdff6c242017-05-09 01:27:14 +0300206 priv->zorder_prop = drm_property_create_range(dev, 0, "zorder", 0,
207 num_planes - 1);
Laurent Pincharte2cd09b2015-03-06 17:16:43 +0200208 if (!priv->zorder_prop)
209 return -ENOMEM;
210
211 return 0;
212}
213
Rob Clarkcd5351f2011-11-12 12:09:40 -0600214static int omap_modeset_init(struct drm_device *dev)
215{
Rob Clarkcd5351f2011-11-12 12:09:40 -0600216 struct omap_drm_private *priv = dev->dev_private;
217 struct omap_dss_device *dssdev = NULL;
Tomi Valkeinen9f759222015-11-05 18:39:52 +0200218 int num_ovls = priv->dispc_ops->get_num_ovls();
219 int num_mgrs = priv->dispc_ops->get_num_mgrs();
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200220 int num_crtcs, crtc_idx, plane_idx;
Laurent Pinchartfb9a35f2015-01-11 16:30:44 +0200221 int ret;
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200222 u32 plane_crtc_mask;
Tomi Valkeinen04b1fc02013-05-14 10:55:19 +0300223
Rob Clarkcd5351f2011-11-12 12:09:40 -0600224 drm_mode_config_init(dev);
225
Laurent Pincharte2cd09b2015-03-06 17:16:43 +0200226 ret = omap_modeset_init_properties(dev);
227 if (ret < 0)
228 return ret;
229
Rob Clarkf5f94542012-12-04 13:59:12 -0600230 /*
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200231 * This function creates exactly one connector, encoder, crtc,
232 * and primary plane per each connected dss-device. Each
233 * connector->encoder->crtc chain is expected to be separate
234 * and each crtc is connect to a single dss-channel. If the
235 * configuration does not match the expectations or exceeds
236 * the available resources, the configuration is rejected.
Rob Clarkf5f94542012-12-04 13:59:12 -0600237 */
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200238 num_crtcs = 0;
Jyri Sarhaf1118b82017-03-24 16:47:51 +0200239 for_each_dss_dev(dssdev)
240 if (omapdss_device_is_connected(dssdev))
241 num_crtcs++;
242
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200243 if (num_crtcs > num_mgrs || num_crtcs > num_ovls ||
244 num_crtcs > ARRAY_SIZE(priv->crtcs) ||
245 num_crtcs > ARRAY_SIZE(priv->planes) ||
246 num_crtcs > ARRAY_SIZE(priv->encoders) ||
247 num_crtcs > ARRAY_SIZE(priv->connectors)) {
248 dev_err(dev->dev, "%s(): Too many connected displays\n",
249 __func__);
250 return -EINVAL;
251 }
252
253 /* All planes can be put to any CRTC */
254 plane_crtc_mask = (1 << num_crtcs) - 1;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600255
Archit Taneja0d8f3712013-03-26 19:15:19 +0530256 dssdev = NULL;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600257
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200258 crtc_idx = 0;
259 plane_idx = 0;
Rob Clarkf5f94542012-12-04 13:59:12 -0600260 for_each_dss_dev(dssdev) {
261 struct drm_connector *connector;
262 struct drm_encoder *encoder;
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200263 struct drm_plane *plane;
264 struct drm_crtc *crtc;
Rob Clarkf5f94542012-12-04 13:59:12 -0600265
Archit Taneja3a01ab22014-01-02 14:49:51 +0530266 if (!omapdss_device_is_connected(dssdev))
Archit Taneja581382e2013-03-26 19:15:18 +0530267 continue;
Tomi Valkeinena7e71e72013-05-08 16:23:32 +0300268
Rob Clarkf5f94542012-12-04 13:59:12 -0600269 encoder = omap_encoder_init(dev, dssdev);
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200270 if (!encoder)
Rob Clarkf5f94542012-12-04 13:59:12 -0600271 return -ENOMEM;
Rob Clarkf5f94542012-12-04 13:59:12 -0600272
273 connector = omap_connector_init(dev,
274 get_connector_type(dssdev), dssdev, encoder);
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200275 if (!connector)
Rob Clarkf5f94542012-12-04 13:59:12 -0600276 return -ENOMEM;
Rob Clarkf5f94542012-12-04 13:59:12 -0600277
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200278 plane = omap_plane_init(dev, plane_idx, DRM_PLANE_TYPE_PRIMARY,
279 plane_crtc_mask);
280 if (IS_ERR(plane))
281 return PTR_ERR(plane);
Rob Clarkf5f94542012-12-04 13:59:12 -0600282
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200283 crtc = omap_crtc_init(dev, plane, dssdev);
284 if (IS_ERR(crtc))
285 return PTR_ERR(crtc);
286
287 drm_mode_connector_attach_encoder(connector, encoder);
288 encoder->possible_crtcs = (1 << crtc_idx);
289
290 priv->crtcs[priv->num_crtcs++] = crtc;
291 priv->planes[priv->num_planes++] = plane;
Rob Clarkf5f94542012-12-04 13:59:12 -0600292 priv->encoders[priv->num_encoders++] = encoder;
293 priv->connectors[priv->num_connectors++] = connector;
294
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200295 plane_idx++;
296 crtc_idx++;
Archit Taneja0d8f3712013-03-26 19:15:19 +0530297 }
298
299 /*
Archit Taneja0d8f3712013-03-26 19:15:19 +0530300 * Create normal planes for the remaining overlays:
301 */
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200302 for (; plane_idx < num_ovls; plane_idx++) {
Laurent Pinchartfb9a35f2015-01-11 16:30:44 +0200303 struct drm_plane *plane;
304
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200305 if (WARN_ON(priv->num_planes >= ARRAY_SIZE(priv->planes)))
306 return -EINVAL;
307
308 plane = omap_plane_init(dev, plane_idx, DRM_PLANE_TYPE_OVERLAY,
309 plane_crtc_mask);
Laurent Pinchartfb9a35f2015-01-11 16:30:44 +0200310 if (IS_ERR(plane))
311 return PTR_ERR(plane);
Archit Taneja0d8f3712013-03-26 19:15:19 +0530312
Archit Taneja0d8f3712013-03-26 19:15:19 +0530313 priv->planes[priv->num_planes++] = plane;
314 }
315
Archit Taneja0d8f3712013-03-26 19:15:19 +0530316 DBG("registered %d planes, %d crtcs, %d encoders and %d connectors\n",
317 priv->num_planes, priv->num_crtcs, priv->num_encoders,
318 priv->num_connectors);
319
Tomi Valkeinen1e907112016-08-23 12:35:39 +0300320 dev->mode_config.min_width = 8;
321 dev->mode_config.min_height = 2;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600322
323 /* note: eventually will need some cpu_is_omapXYZ() type stuff here
324 * to fill in these limits properly on different OMAP generations..
325 */
326 dev->mode_config.max_width = 2048;
327 dev->mode_config.max_height = 2048;
328
329 dev->mode_config.funcs = &omap_mode_config_funcs;
Laurent Pincharta9e6f9f2017-05-09 01:27:10 +0300330 dev->mode_config.helper_private = &omap_mode_config_helper_funcs;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600331
Laurent Pinchart69a12262015-03-05 21:38:16 +0200332 drm_mode_config_reset(dev);
333
Laurent Pinchart728ae8d2015-05-28 00:21:29 +0300334 omap_drm_irq_install(dev);
335
Rob Clarkcd5351f2011-11-12 12:09:40 -0600336 return 0;
337}
338
Rob Clarkcd5351f2011-11-12 12:09:40 -0600339/*
Peter Ujfalusi3c596802017-06-02 15:26:35 +0300340 * Enable the HPD in external components if supported
341 */
342static void omap_modeset_enable_external_hpd(void)
343{
344 struct omap_dss_device *dssdev = NULL;
345
346 for_each_dss_dev(dssdev) {
347 if (dssdev->driver->enable_hpd)
348 dssdev->driver->enable_hpd(dssdev);
349 }
350}
351
352/*
353 * Disable the HPD in external components if supported
354 */
355static void omap_modeset_disable_external_hpd(void)
356{
357 struct omap_dss_device *dssdev = NULL;
358
359 for_each_dss_dev(dssdev) {
360 if (dssdev->driver->disable_hpd)
361 dssdev->driver->disable_hpd(dssdev);
362 }
363}
364
365/*
Rob Clarkcd5351f2011-11-12 12:09:40 -0600366 * drm ioctl funcs
367 */
368
369
370static int ioctl_get_param(struct drm_device *dev, void *data,
371 struct drm_file *file_priv)
372{
Rob Clark5e3b0872012-10-29 09:31:12 +0100373 struct omap_drm_private *priv = dev->dev_private;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600374 struct drm_omap_param *args = data;
375
376 DBG("%p: param=%llu", dev, args->param);
377
378 switch (args->param) {
379 case OMAP_PARAM_CHIPSET_ID:
Rob Clark5e3b0872012-10-29 09:31:12 +0100380 args->value = priv->omaprev;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600381 break;
382 default:
383 DBG("unknown parameter %lld", args->param);
384 return -EINVAL;
385 }
386
387 return 0;
388}
389
390static int ioctl_set_param(struct drm_device *dev, void *data,
391 struct drm_file *file_priv)
392{
393 struct drm_omap_param *args = data;
394
395 switch (args->param) {
396 default:
397 DBG("unknown parameter %lld", args->param);
398 return -EINVAL;
399 }
400
401 return 0;
402}
403
Laurent Pinchartef3f4e92015-12-14 22:39:36 +0200404#define OMAP_BO_USER_MASK 0x00ffffff /* flags settable by userspace */
405
Rob Clarkcd5351f2011-11-12 12:09:40 -0600406static int ioctl_gem_new(struct drm_device *dev, void *data,
407 struct drm_file *file_priv)
408{
409 struct drm_omap_gem_new *args = data;
Laurent Pinchartef3f4e92015-12-14 22:39:36 +0200410 u32 flags = args->flags & OMAP_BO_USER_MASK;
411
Rob Clarkf5f94542012-12-04 13:59:12 -0600412 VERB("%p:%p: size=0x%08x, flags=%08x", dev, file_priv,
Laurent Pinchartef3f4e92015-12-14 22:39:36 +0200413 args->size.bytes, flags);
414
415 return omap_gem_new_handle(dev, file_priv, args->size, flags,
416 &args->handle);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600417}
418
Rob Clarkcd5351f2011-11-12 12:09:40 -0600419static int ioctl_gem_info(struct drm_device *dev, void *data,
420 struct drm_file *file_priv)
421{
422 struct drm_omap_gem_info *args = data;
423 struct drm_gem_object *obj;
424 int ret = 0;
425
Rob Clarkf5f94542012-12-04 13:59:12 -0600426 VERB("%p:%p: handle=%d", dev, file_priv, args->handle);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600427
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100428 obj = drm_gem_object_lookup(file_priv, args->handle);
YAMANE Toshiakic7f904b2012-11-14 19:30:38 +0900429 if (!obj)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600430 return -ENOENT;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600431
Rob Clarkf7f9f452011-12-05 19:19:22 -0600432 args->size = omap_gem_mmap_size(obj);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600433 args->offset = omap_gem_mmap_offset(obj);
434
435 drm_gem_object_unreference_unlocked(obj);
436
437 return ret;
438}
439
Rob Clarkbaa70942013-08-02 13:27:49 -0400440static const struct drm_ioctl_desc ioctls[DRM_COMMAND_END - DRM_COMMAND_BASE] = {
Hemant Hariyani5f6ab8c2016-06-07 13:23:19 -0500441 DRM_IOCTL_DEF_DRV(OMAP_GET_PARAM, ioctl_get_param,
442 DRM_AUTH | DRM_RENDER_ALLOW),
443 DRM_IOCTL_DEF_DRV(OMAP_SET_PARAM, ioctl_set_param,
444 DRM_AUTH | DRM_MASTER | DRM_ROOT_ONLY),
445 DRM_IOCTL_DEF_DRV(OMAP_GEM_NEW, ioctl_gem_new,
446 DRM_AUTH | DRM_RENDER_ALLOW),
Laurent Pinchartd6f544f2017-05-09 01:27:11 +0300447 /* Deprecated, to be removed. */
448 DRM_IOCTL_DEF_DRV(OMAP_GEM_CPU_PREP, drm_noop,
Hemant Hariyani5f6ab8c2016-06-07 13:23:19 -0500449 DRM_AUTH | DRM_RENDER_ALLOW),
Laurent Pinchartd6f544f2017-05-09 01:27:11 +0300450 /* Deprecated, to be removed. */
451 DRM_IOCTL_DEF_DRV(OMAP_GEM_CPU_FINI, drm_noop,
Hemant Hariyani5f6ab8c2016-06-07 13:23:19 -0500452 DRM_AUTH | DRM_RENDER_ALLOW),
453 DRM_IOCTL_DEF_DRV(OMAP_GEM_INFO, ioctl_gem_info,
454 DRM_AUTH | DRM_RENDER_ALLOW),
Rob Clarkcd5351f2011-11-12 12:09:40 -0600455};
456
457/*
458 * drm driver funcs
459 */
460
Rob Clarkcd5351f2011-11-12 12:09:40 -0600461static int dev_open(struct drm_device *dev, struct drm_file *file)
462{
463 file->driver_priv = NULL;
464
465 DBG("open: dev=%p, file=%p", dev, file);
466
467 return 0;
468}
469
Rob Clarkcd5351f2011-11-12 12:09:40 -0600470/**
471 * lastclose - clean up after all DRM clients have exited
472 * @dev: DRM device
473 *
474 * Take care of cleaning up after all DRM clients have exited. In the
475 * mode setting case, we want to restore the kernel's initial mode (just
476 * in case the last client left us in a bad state).
477 */
478static void dev_lastclose(struct drm_device *dev)
479{
Rob Clarkcd5351f2011-11-12 12:09:40 -0600480 struct omap_drm_private *priv = dev->dev_private;
481 int ret;
482
483 DBG("lastclose: dev=%p", dev);
484
Tomi Valkeinenc7c1aec2014-09-25 19:24:26 +0000485 if (priv->fbdev) {
486 ret = drm_fb_helper_restore_fbdev_mode_unlocked(priv->fbdev);
487 if (ret)
488 DBG("failed to restore crtc mode");
489 }
Rob Clarkcd5351f2011-11-12 12:09:40 -0600490}
491
Laurent Pinchart78b68552012-05-17 13:27:22 +0200492static const struct vm_operations_struct omap_gem_vm_ops = {
Rob Clarkcd5351f2011-11-12 12:09:40 -0600493 .fault = omap_gem_fault,
494 .open = drm_gem_vm_open,
495 .close = drm_gem_vm_close,
496};
497
Rob Clarkff4f3872012-01-16 12:51:14 -0600498static const struct file_operations omapdriver_fops = {
Laurent Pinchart222025e2015-01-11 00:02:07 +0200499 .owner = THIS_MODULE,
500 .open = drm_open,
501 .unlocked_ioctl = drm_ioctl,
Tomi Valkeinen9d24159a2017-02-24 13:24:50 +0200502 .compat_ioctl = drm_compat_ioctl,
Laurent Pinchart222025e2015-01-11 00:02:07 +0200503 .release = drm_release,
504 .mmap = omap_gem_mmap,
505 .poll = drm_poll,
506 .read = drm_read,
507 .llseek = noop_llseek,
Rob Clarkff4f3872012-01-16 12:51:14 -0600508};
509
Rob Clarkcd5351f2011-11-12 12:09:40 -0600510static struct drm_driver omap_drm_driver = {
Tomi Valkeinen728fea72015-10-02 11:10:41 +0300511 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME |
Hemant Hariyani5f6ab8c2016-06-07 13:23:19 -0500512 DRIVER_ATOMIC | DRIVER_RENDER,
Laurent Pinchart222025e2015-01-11 00:02:07 +0200513 .open = dev_open,
514 .lastclose = dev_lastclose,
Andy Gross6169a1482011-12-15 21:05:17 -0600515#ifdef CONFIG_DEBUG_FS
Laurent Pinchart222025e2015-01-11 00:02:07 +0200516 .debugfs_init = omap_debugfs_init,
Andy Gross6169a1482011-12-15 21:05:17 -0600517#endif
Laurent Pinchart222025e2015-01-11 00:02:07 +0200518 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
519 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
520 .gem_prime_export = omap_gem_prime_export,
521 .gem_prime_import = omap_gem_prime_import,
522 .gem_free_object = omap_gem_free_object,
523 .gem_vm_ops = &omap_gem_vm_ops,
524 .dumb_create = omap_gem_dumb_create,
525 .dumb_map_offset = omap_gem_dumb_map_offset,
Laurent Pinchart222025e2015-01-11 00:02:07 +0200526 .ioctls = ioctls,
527 .num_ioctls = DRM_OMAP_NUM_IOCTLS,
528 .fops = &omapdriver_fops,
529 .name = DRIVER_NAME,
530 .desc = DRIVER_DESC,
531 .date = DRIVER_DATE,
532 .major = DRIVER_MAJOR,
533 .minor = DRIVER_MINOR,
534 .patchlevel = DRIVER_PATCHLEVEL,
Rob Clarkcd5351f2011-11-12 12:09:40 -0600535};
536
Laurent Pinchart6e471fa2017-05-06 02:57:12 +0300537static const struct soc_device_attribute omapdrm_soc_devices[] = {
538 { .family = "OMAP3", .data = (void *)0x3430 },
539 { .family = "OMAP4", .data = (void *)0x4430 },
540 { .family = "OMAP5", .data = (void *)0x5430 },
541 { .family = "DRA7", .data = (void *)0x0752 },
542 { /* sentinel */ }
543};
544
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200545static int pdev_probe(struct platform_device *pdev)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600546{
Laurent Pinchart6e471fa2017-05-06 02:57:12 +0300547 const struct soc_device_attribute *soc;
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200548 struct omap_drm_private *priv;
549 struct drm_device *ddev;
550 unsigned int i;
551 int ret;
552
553 DBG("%s", pdev->name);
Archit Taneja3a01ab22014-01-02 14:49:51 +0530554
Tomi Valkeinen591a0ac2013-05-23 12:07:50 +0300555 if (omapdss_is_initialized() == false)
556 return -EPROBE_DEFER;
557
Laurent Pinchart510c74c2017-08-11 16:49:08 +0300558 ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
559 if (ret) {
560 dev_err(&pdev->dev, "Failed to set the DMA mask\n");
561 return ret;
562 }
563
Archit Taneja3a01ab22014-01-02 14:49:51 +0530564 omap_crtc_pre_init();
565
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200566 ret = omap_connect_dssdevs();
567 if (ret)
568 goto err_crtc_uninit;
569
570 /* Allocate and initialize the driver private structure. */
571 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
572 if (!priv) {
573 ret = -ENOMEM;
574 goto err_disconnect_dssdevs;
Archit Taneja3a01ab22014-01-02 14:49:51 +0530575 }
576
Tomi Valkeinen9f759222015-11-05 18:39:52 +0200577 priv->dispc_ops = dispc_get_ops();
578
Laurent Pinchart6e471fa2017-05-06 02:57:12 +0300579 soc = soc_device_match(omapdrm_soc_devices);
580 priv->omaprev = soc ? (unsigned int)soc->data : 0;
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200581 priv->wq = alloc_ordered_workqueue("omapdrm", 0);
582
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200583 spin_lock_init(&priv->list_lock);
584 INIT_LIST_HEAD(&priv->obj_list);
585
586 /* Allocate and initialize the DRM device. */
587 ddev = drm_dev_alloc(&omap_drm_driver, &pdev->dev);
588 if (IS_ERR(ddev)) {
589 ret = PTR_ERR(ddev);
590 goto err_free_priv;
591 }
592
593 ddev->dev_private = priv;
594 platform_set_drvdata(pdev, ddev);
595
596 omap_gem_init(ddev);
597
598 ret = omap_modeset_init(ddev);
599 if (ret) {
600 dev_err(&pdev->dev, "omap_modeset_init failed: ret=%d\n", ret);
601 goto err_free_drm_dev;
602 }
603
604 /* Initialize vblank handling, start with all CRTCs disabled. */
605 ret = drm_vblank_init(ddev, priv->num_crtcs);
606 if (ret) {
607 dev_err(&pdev->dev, "could not init vblank\n");
608 goto err_cleanup_modeset;
609 }
610
611 for (i = 0; i < priv->num_crtcs; i++)
612 drm_crtc_vblank_off(priv->crtcs[i]);
613
614 priv->fbdev = omap_fbdev_init(ddev);
615
616 drm_kms_helper_poll_init(ddev);
Peter Ujfalusi3c596802017-06-02 15:26:35 +0300617 omap_modeset_enable_external_hpd();
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200618
619 /*
620 * Register the DRM device with the core and the connectors with
621 * sysfs.
622 */
623 ret = drm_dev_register(ddev, 0);
624 if (ret)
625 goto err_cleanup_helpers;
626
627 return 0;
628
629err_cleanup_helpers:
Peter Ujfalusi3c596802017-06-02 15:26:35 +0300630 omap_modeset_disable_external_hpd();
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200631 drm_kms_helper_poll_fini(ddev);
632 if (priv->fbdev)
633 omap_fbdev_free(ddev);
634err_cleanup_modeset:
635 drm_mode_config_cleanup(ddev);
636 omap_drm_irq_uninstall(ddev);
637err_free_drm_dev:
638 omap_gem_deinit(ddev);
639 drm_dev_unref(ddev);
640err_free_priv:
641 destroy_workqueue(priv->wq);
642 kfree(priv);
643err_disconnect_dssdevs:
644 omap_disconnect_dssdevs();
645err_crtc_uninit:
646 omap_crtc_pre_uninit();
647 return ret;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600648}
649
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200650static int pdev_remove(struct platform_device *pdev)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600651{
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200652 struct drm_device *ddev = platform_get_drvdata(pdev);
653 struct omap_drm_private *priv = ddev->dev_private;
654
Rob Clarkcd5351f2011-11-12 12:09:40 -0600655 DBG("");
Andy Gross5c137792012-03-05 10:48:39 -0600656
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200657 drm_dev_unregister(ddev);
658
Peter Ujfalusi3c596802017-06-02 15:26:35 +0300659 omap_modeset_disable_external_hpd();
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200660 drm_kms_helper_poll_fini(ddev);
661
662 if (priv->fbdev)
663 omap_fbdev_free(ddev);
664
Tomi Valkeinen8a54aa92017-03-27 10:02:22 +0300665 drm_atomic_helper_shutdown(ddev);
666
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200667 drm_mode_config_cleanup(ddev);
668
669 omap_drm_irq_uninstall(ddev);
670 omap_gem_deinit(ddev);
671
672 drm_dev_unref(ddev);
673
674 destroy_workqueue(priv->wq);
675 kfree(priv);
Tomi Valkeinen707cf582014-04-02 13:47:43 +0300676
Archit Tanejacc823bd2014-01-02 14:49:52 +0530677 omap_disconnect_dssdevs();
678 omap_crtc_pre_uninit();
Daniel Vetterfd3c0252013-12-11 11:34:26 +0100679
Rob Clarkcd5351f2011-11-12 12:09:40 -0600680 return 0;
681}
682
Grygorii Strashko8450c8d2015-02-26 15:57:17 +0200683#ifdef CONFIG_PM_SLEEP
Tomi Valkeinen92bf0f92015-10-02 11:10:42 +0300684static int omap_drm_suspend_all_displays(void)
685{
686 struct omap_dss_device *dssdev = NULL;
687
688 for_each_dss_dev(dssdev) {
689 if (!dssdev->driver)
690 continue;
691
692 if (dssdev->state == OMAP_DSS_DISPLAY_ACTIVE) {
693 dssdev->driver->disable(dssdev);
694 dssdev->activate_after_resume = true;
695 } else {
696 dssdev->activate_after_resume = false;
697 }
698 }
699
700 return 0;
701}
702
703static int omap_drm_resume_all_displays(void)
704{
705 struct omap_dss_device *dssdev = NULL;
706
707 for_each_dss_dev(dssdev) {
708 if (!dssdev->driver)
709 continue;
710
711 if (dssdev->activate_after_resume) {
712 dssdev->driver->enable(dssdev);
713 dssdev->activate_after_resume = false;
714 }
715 }
716
717 return 0;
718}
719
Tomi Valkeinenccd7b5e2014-11-14 15:18:28 +0200720static int omap_drm_suspend(struct device *dev)
721{
722 struct drm_device *drm_dev = dev_get_drvdata(dev);
723
724 drm_kms_helper_poll_disable(drm_dev);
725
Tomi Valkeinen92bf0f92015-10-02 11:10:42 +0300726 drm_modeset_lock_all(drm_dev);
727 omap_drm_suspend_all_displays();
728 drm_modeset_unlock_all(drm_dev);
729
Tomi Valkeinenccd7b5e2014-11-14 15:18:28 +0200730 return 0;
731}
732
733static int omap_drm_resume(struct device *dev)
734{
735 struct drm_device *drm_dev = dev_get_drvdata(dev);
736
Tomi Valkeinen92bf0f92015-10-02 11:10:42 +0300737 drm_modeset_lock_all(drm_dev);
738 omap_drm_resume_all_displays();
739 drm_modeset_unlock_all(drm_dev);
740
Tomi Valkeinenccd7b5e2014-11-14 15:18:28 +0200741 drm_kms_helper_poll_enable(drm_dev);
742
743 return omap_gem_resume(dev);
744}
Andy Grosse78edba2012-12-19 14:53:37 -0600745#endif
746
Grygorii Strashko8450c8d2015-02-26 15:57:17 +0200747static SIMPLE_DEV_PM_OPS(omapdrm_pm_ops, omap_drm_suspend, omap_drm_resume);
748
Tomi Valkeinen6717cd22013-04-10 10:44:00 +0300749static struct platform_driver pdev = {
Laurent Pinchart222025e2015-01-11 00:02:07 +0200750 .driver = {
Tomi Valkeinenf64eafa2017-08-16 12:43:55 +0300751 .name = "omapdrm",
Laurent Pinchart222025e2015-01-11 00:02:07 +0200752 .pm = &omapdrm_pm_ops,
Laurent Pinchart222025e2015-01-11 00:02:07 +0200753 },
754 .probe = pdev_probe,
755 .remove = pdev_remove,
Rob Clarkcd5351f2011-11-12 12:09:40 -0600756};
757
Thierry Redinge1c49bd2015-12-02 17:23:31 +0100758static struct platform_driver * const drivers[] = {
759 &omap_dmm_driver,
760 &pdev,
761};
762
Rob Clarkcd5351f2011-11-12 12:09:40 -0600763static int __init omap_drm_init(void)
764{
765 DBG("init");
Tomi Valkeinenea7e3a62014-04-02 14:31:50 +0300766
Thierry Redinge1c49bd2015-12-02 17:23:31 +0100767 return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
Rob Clarkcd5351f2011-11-12 12:09:40 -0600768}
769
770static void __exit omap_drm_fini(void)
771{
772 DBG("fini");
Tomi Valkeinenea7e3a62014-04-02 14:31:50 +0300773
Thierry Redinge1c49bd2015-12-02 17:23:31 +0100774 platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
Rob Clarkcd5351f2011-11-12 12:09:40 -0600775}
776
777/* need late_initcall() so we load after dss_driver's are loaded */
778late_initcall(omap_drm_init);
779module_exit(omap_drm_fini);
780
781MODULE_AUTHOR("Rob Clark <rob@ti.com>");
782MODULE_DESCRIPTION("OMAP DRM Display Driver");
783MODULE_ALIAS("platform:" DRIVER_NAME);
784MODULE_LICENSE("GPL v2");