blob: e2e935813cc438de3ea57f7077c64e63dcebcb68 [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 Pinchart748471a52015-03-05 23:42:39 +020020#include <drm/drm_atomic.h>
Laurent Pinchartcef77d42015-03-05 21:50:00 +020021#include <drm/drm_atomic_helper.h>
Laurent Pinchart2d278f52015-03-05 21:31:37 +020022#include <drm/drm_crtc_helper.h>
23#include <drm/drm_fb_helper.h>
Rob Clarkcd5351f2011-11-12 12:09:40 -060024
Andy Gross5c137792012-03-05 10:48:39 -060025#include "omap_dmm_tiler.h"
Laurent Pinchart2d278f52015-03-05 21:31:37 +020026#include "omap_drv.h"
Rob Clarkcd5351f2011-11-12 12:09:40 -060027
28#define DRIVER_NAME MODULE_NAME
29#define DRIVER_DESC "OMAP DRM"
30#define DRIVER_DATE "20110917"
31#define DRIVER_MAJOR 1
32#define DRIVER_MINOR 0
33#define DRIVER_PATCHLEVEL 0
34
Rob Clarkcd5351f2011-11-12 12:09:40 -060035/*
36 * mode config funcs
37 */
38
39/* Notes about mapping DSS and DRM entities:
40 * CRTC: overlay
41 * encoder: manager.. with some extension to allow one primary CRTC
42 * and zero or more video CRTC's to be mapped to one encoder?
43 * connector: dssdev.. manager can be attached/detached from different
44 * devices
45 */
46
47static void omap_fb_output_poll_changed(struct drm_device *dev)
48{
49 struct omap_drm_private *priv = dev->dev_private;
50 DBG("dev=%p", dev);
YAMANE Toshiakic7f904b2012-11-14 19:30:38 +090051 if (priv->fbdev)
Rob Clarkcd5351f2011-11-12 12:09:40 -060052 drm_fb_helper_hotplug_event(priv->fbdev);
Rob Clarkcd5351f2011-11-12 12:09:40 -060053}
54
Tomi Valkeinen5f741b32015-05-29 16:01:18 +030055static void omap_atomic_wait_for_completion(struct drm_device *dev,
56 struct drm_atomic_state *old_state)
57{
58 struct drm_crtc_state *old_crtc_state;
59 struct drm_crtc *crtc;
60 unsigned int i;
61 int ret;
62
63 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
64 if (!crtc->state->enable)
65 continue;
66
67 ret = omap_crtc_wait_pending(crtc);
68
69 if (!ret)
70 dev_warn(dev->dev,
71 "atomic complete timeout (pipe %u)!\n", i);
72 }
73}
74
Laurent Pincharta9e6f9f2017-05-09 01:27:10 +030075static void omap_atomic_commit_tail(struct drm_atomic_state *old_state)
Laurent Pinchart748471a52015-03-05 23:42:39 +020076{
Laurent Pincharta9e6f9f2017-05-09 01:27:10 +030077 struct drm_device *dev = old_state->dev;
Laurent Pinchart748471a52015-03-05 23:42:39 +020078 struct omap_drm_private *priv = dev->dev_private;
Laurent Pinchart748471a52015-03-05 23:42:39 +020079
Tomi Valkeinen9f759222015-11-05 18:39:52 +020080 priv->dispc_ops->runtime_get();
Laurent Pinchart69fb7c82015-05-28 02:09:56 +030081
Laurent Pincharta9e6f9f2017-05-09 01:27:10 +030082 /* Apply the atomic update. */
Laurent Pinchart748471a52015-03-05 23:42:39 +020083 drm_atomic_helper_commit_modeset_disables(dev, old_state);
Jyri Sarha897145d2017-01-27 12:04:55 +020084
85 /* With the current dss dispc implementation we have to enable
86 * the new modeset before we can commit planes. The dispc ovl
87 * configuration relies on the video mode configuration been
88 * written into the HW when the ovl configuration is
89 * calculated.
90 *
91 * This approach is not ideal because after a mode change the
92 * plane update is executed only after the first vblank
93 * interrupt. The dispc implementation should be fixed so that
94 * it is able use uncommitted drm state information.
95 */
Laurent Pinchart748471a52015-03-05 23:42:39 +020096 drm_atomic_helper_commit_modeset_enables(dev, old_state);
Jyri Sarha897145d2017-01-27 12:04:55 +020097 omap_atomic_wait_for_completion(dev, old_state);
98
99 drm_atomic_helper_commit_planes(dev, old_state, 0);
Laurent Pinchart748471a52015-03-05 23:42:39 +0200100
Laurent Pincharta9e6f9f2017-05-09 01:27:10 +0300101 drm_atomic_helper_commit_hw_done(old_state);
102
103 /*
104 * Wait for completion of the page flips to ensure that old buffers
105 * can't be touched by the hardware anymore before cleaning up planes.
106 */
Tomi Valkeinen5f741b32015-05-29 16:01:18 +0300107 omap_atomic_wait_for_completion(dev, old_state);
Laurent Pinchart748471a52015-03-05 23:42:39 +0200108
109 drm_atomic_helper_cleanup_planes(dev, old_state);
110
Tomi Valkeinen9f759222015-11-05 18:39:52 +0200111 priv->dispc_ops->runtime_put();
Laurent Pinchart748471a52015-03-05 23:42:39 +0200112}
113
Laurent Pincharta9e6f9f2017-05-09 01:27:10 +0300114static const struct drm_mode_config_helper_funcs omap_mode_config_helper_funcs = {
115 .atomic_commit_tail = omap_atomic_commit_tail,
116};
Laurent Pinchart748471a52015-03-05 23:42:39 +0200117
Laurent Pincharte6ecefa2012-05-17 13:27:23 +0200118static const struct drm_mode_config_funcs omap_mode_config_funcs = {
Rob Clarkcd5351f2011-11-12 12:09:40 -0600119 .fb_create = omap_framebuffer_create,
120 .output_poll_changed = omap_fb_output_poll_changed,
Laurent Pinchartcef77d42015-03-05 21:50:00 +0200121 .atomic_check = drm_atomic_helper_check,
Laurent Pincharta9e6f9f2017-05-09 01:27:10 +0300122 .atomic_commit = drm_atomic_helper_commit,
Rob Clarkcd5351f2011-11-12 12:09:40 -0600123};
124
125static int get_connector_type(struct omap_dss_device *dssdev)
126{
127 switch (dssdev->type) {
128 case OMAP_DISPLAY_TYPE_HDMI:
129 return DRM_MODE_CONNECTOR_HDMIA;
Tomi Valkeinen4635c172013-05-14 14:14:15 +0300130 case OMAP_DISPLAY_TYPE_DVI:
131 return DRM_MODE_CONNECTOR_DVID;
Sebastian Reichel4a64b902016-03-08 17:39:36 +0100132 case OMAP_DISPLAY_TYPE_DSI:
133 return DRM_MODE_CONNECTOR_DSI;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600134 default:
135 return DRM_MODE_CONNECTOR_Unknown;
136 }
137}
138
Archit Tanejacc823bd2014-01-02 14:49:52 +0530139static void omap_disconnect_dssdevs(void)
140{
141 struct omap_dss_device *dssdev = NULL;
142
143 for_each_dss_dev(dssdev)
144 dssdev->driver->disconnect(dssdev);
145}
Archit Taneja0d8f3712013-03-26 19:15:19 +0530146
Archit Taneja3a01ab22014-01-02 14:49:51 +0530147static int omap_connect_dssdevs(void)
148{
149 int r;
150 struct omap_dss_device *dssdev = NULL;
Peter Ujfalusia09d2bc2016-05-03 22:08:01 +0300151
152 if (!omapdss_stack_is_ready())
153 return -EPROBE_DEFER;
Archit Taneja3a01ab22014-01-02 14:49:51 +0530154
155 for_each_dss_dev(dssdev) {
156 r = dssdev->driver->connect(dssdev);
157 if (r == -EPROBE_DEFER) {
158 omap_dss_put_device(dssdev);
159 goto cleanup;
160 } else if (r) {
161 dev_warn(dssdev->dev, "could not connect display: %s\n",
162 dssdev->name);
Archit Taneja3a01ab22014-01-02 14:49:51 +0530163 }
164 }
165
Archit Taneja3a01ab22014-01-02 14:49:51 +0530166 return 0;
167
168cleanup:
169 /*
170 * if we are deferring probe, we disconnect the devices we previously
171 * connected
172 */
Archit Tanejacc823bd2014-01-02 14:49:52 +0530173 omap_disconnect_dssdevs();
Archit Taneja3a01ab22014-01-02 14:49:51 +0530174
175 return r;
176}
Rob Clarkcd5351f2011-11-12 12:09:40 -0600177
Laurent Pincharte2cd09b2015-03-06 17:16:43 +0200178static int omap_modeset_init_properties(struct drm_device *dev)
179{
180 struct omap_drm_private *priv = dev->dev_private;
181
Laurent Pincharte2cd09b2015-03-06 17:16:43 +0200182 priv->zorder_prop = drm_property_create_range(dev, 0, "zorder", 0, 3);
183 if (!priv->zorder_prop)
184 return -ENOMEM;
185
186 return 0;
187}
188
Rob Clarkcd5351f2011-11-12 12:09:40 -0600189static int omap_modeset_init(struct drm_device *dev)
190{
Rob Clarkcd5351f2011-11-12 12:09:40 -0600191 struct omap_drm_private *priv = dev->dev_private;
192 struct omap_dss_device *dssdev = NULL;
Tomi Valkeinen9f759222015-11-05 18:39:52 +0200193 int num_ovls = priv->dispc_ops->get_num_ovls();
194 int num_mgrs = priv->dispc_ops->get_num_mgrs();
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200195 int num_crtcs, crtc_idx, plane_idx;
Laurent Pinchartfb9a35f2015-01-11 16:30:44 +0200196 int ret;
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200197 u32 plane_crtc_mask;
Tomi Valkeinen04b1fc02013-05-14 10:55:19 +0300198
Rob Clarkcd5351f2011-11-12 12:09:40 -0600199 drm_mode_config_init(dev);
200
Laurent Pincharte2cd09b2015-03-06 17:16:43 +0200201 ret = omap_modeset_init_properties(dev);
202 if (ret < 0)
203 return ret;
204
Rob Clarkf5f94542012-12-04 13:59:12 -0600205 /*
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200206 * This function creates exactly one connector, encoder, crtc,
207 * and primary plane per each connected dss-device. Each
208 * connector->encoder->crtc chain is expected to be separate
209 * and each crtc is connect to a single dss-channel. If the
210 * configuration does not match the expectations or exceeds
211 * the available resources, the configuration is rejected.
Rob Clarkf5f94542012-12-04 13:59:12 -0600212 */
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200213 num_crtcs = 0;
Jyri Sarhaf1118b82017-03-24 16:47:51 +0200214 for_each_dss_dev(dssdev)
215 if (omapdss_device_is_connected(dssdev))
216 num_crtcs++;
217
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200218 if (num_crtcs > num_mgrs || num_crtcs > num_ovls ||
219 num_crtcs > ARRAY_SIZE(priv->crtcs) ||
220 num_crtcs > ARRAY_SIZE(priv->planes) ||
221 num_crtcs > ARRAY_SIZE(priv->encoders) ||
222 num_crtcs > ARRAY_SIZE(priv->connectors)) {
223 dev_err(dev->dev, "%s(): Too many connected displays\n",
224 __func__);
225 return -EINVAL;
226 }
227
228 /* All planes can be put to any CRTC */
229 plane_crtc_mask = (1 << num_crtcs) - 1;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600230
Archit Taneja0d8f3712013-03-26 19:15:19 +0530231 dssdev = NULL;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600232
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200233 crtc_idx = 0;
234 plane_idx = 0;
Rob Clarkf5f94542012-12-04 13:59:12 -0600235 for_each_dss_dev(dssdev) {
236 struct drm_connector *connector;
237 struct drm_encoder *encoder;
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200238 struct drm_plane *plane;
239 struct drm_crtc *crtc;
Rob Clarkf5f94542012-12-04 13:59:12 -0600240
Archit Taneja3a01ab22014-01-02 14:49:51 +0530241 if (!omapdss_device_is_connected(dssdev))
Archit Taneja581382e2013-03-26 19:15:18 +0530242 continue;
Tomi Valkeinena7e71e72013-05-08 16:23:32 +0300243
Rob Clarkf5f94542012-12-04 13:59:12 -0600244 encoder = omap_encoder_init(dev, dssdev);
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200245 if (!encoder)
Rob Clarkf5f94542012-12-04 13:59:12 -0600246 return -ENOMEM;
Rob Clarkf5f94542012-12-04 13:59:12 -0600247
248 connector = omap_connector_init(dev,
249 get_connector_type(dssdev), dssdev, encoder);
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200250 if (!connector)
Rob Clarkf5f94542012-12-04 13:59:12 -0600251 return -ENOMEM;
Rob Clarkf5f94542012-12-04 13:59:12 -0600252
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200253 plane = omap_plane_init(dev, plane_idx, DRM_PLANE_TYPE_PRIMARY,
254 plane_crtc_mask);
255 if (IS_ERR(plane))
256 return PTR_ERR(plane);
Rob Clarkf5f94542012-12-04 13:59:12 -0600257
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200258 crtc = omap_crtc_init(dev, plane, dssdev);
259 if (IS_ERR(crtc))
260 return PTR_ERR(crtc);
261
262 drm_mode_connector_attach_encoder(connector, encoder);
263 encoder->possible_crtcs = (1 << crtc_idx);
264
265 priv->crtcs[priv->num_crtcs++] = crtc;
266 priv->planes[priv->num_planes++] = plane;
Rob Clarkf5f94542012-12-04 13:59:12 -0600267 priv->encoders[priv->num_encoders++] = encoder;
268 priv->connectors[priv->num_connectors++] = connector;
269
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200270 plane_idx++;
271 crtc_idx++;
Archit Taneja0d8f3712013-03-26 19:15:19 +0530272 }
273
274 /*
Archit Taneja0d8f3712013-03-26 19:15:19 +0530275 * Create normal planes for the remaining overlays:
276 */
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200277 for (; plane_idx < num_ovls; plane_idx++) {
Laurent Pinchartfb9a35f2015-01-11 16:30:44 +0200278 struct drm_plane *plane;
279
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200280 if (WARN_ON(priv->num_planes >= ARRAY_SIZE(priv->planes)))
281 return -EINVAL;
282
283 plane = omap_plane_init(dev, plane_idx, DRM_PLANE_TYPE_OVERLAY,
284 plane_crtc_mask);
Laurent Pinchartfb9a35f2015-01-11 16:30:44 +0200285 if (IS_ERR(plane))
286 return PTR_ERR(plane);
Archit Taneja0d8f3712013-03-26 19:15:19 +0530287
Archit Taneja0d8f3712013-03-26 19:15:19 +0530288 priv->planes[priv->num_planes++] = plane;
289 }
290
Archit Taneja0d8f3712013-03-26 19:15:19 +0530291 DBG("registered %d planes, %d crtcs, %d encoders and %d connectors\n",
292 priv->num_planes, priv->num_crtcs, priv->num_encoders,
293 priv->num_connectors);
294
Tomi Valkeinen1e907112016-08-23 12:35:39 +0300295 dev->mode_config.min_width = 8;
296 dev->mode_config.min_height = 2;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600297
298 /* note: eventually will need some cpu_is_omapXYZ() type stuff here
299 * to fill in these limits properly on different OMAP generations..
300 */
301 dev->mode_config.max_width = 2048;
302 dev->mode_config.max_height = 2048;
303
304 dev->mode_config.funcs = &omap_mode_config_funcs;
Laurent Pincharta9e6f9f2017-05-09 01:27:10 +0300305 dev->mode_config.helper_private = &omap_mode_config_helper_funcs;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600306
Laurent Pinchart69a12262015-03-05 21:38:16 +0200307 drm_mode_config_reset(dev);
308
Laurent Pinchart728ae8d2015-05-28 00:21:29 +0300309 omap_drm_irq_install(dev);
310
Rob Clarkcd5351f2011-11-12 12:09:40 -0600311 return 0;
312}
313
Rob Clarkcd5351f2011-11-12 12:09:40 -0600314/*
315 * drm ioctl funcs
316 */
317
318
319static int ioctl_get_param(struct drm_device *dev, void *data,
320 struct drm_file *file_priv)
321{
Rob Clark5e3b0872012-10-29 09:31:12 +0100322 struct omap_drm_private *priv = dev->dev_private;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600323 struct drm_omap_param *args = data;
324
325 DBG("%p: param=%llu", dev, args->param);
326
327 switch (args->param) {
328 case OMAP_PARAM_CHIPSET_ID:
Rob Clark5e3b0872012-10-29 09:31:12 +0100329 args->value = priv->omaprev;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600330 break;
331 default:
332 DBG("unknown parameter %lld", args->param);
333 return -EINVAL;
334 }
335
336 return 0;
337}
338
339static int ioctl_set_param(struct drm_device *dev, void *data,
340 struct drm_file *file_priv)
341{
342 struct drm_omap_param *args = data;
343
344 switch (args->param) {
345 default:
346 DBG("unknown parameter %lld", args->param);
347 return -EINVAL;
348 }
349
350 return 0;
351}
352
Laurent Pinchartef3f4e92015-12-14 22:39:36 +0200353#define OMAP_BO_USER_MASK 0x00ffffff /* flags settable by userspace */
354
Rob Clarkcd5351f2011-11-12 12:09:40 -0600355static int ioctl_gem_new(struct drm_device *dev, void *data,
356 struct drm_file *file_priv)
357{
358 struct drm_omap_gem_new *args = data;
Laurent Pinchartef3f4e92015-12-14 22:39:36 +0200359 u32 flags = args->flags & OMAP_BO_USER_MASK;
360
Rob Clarkf5f94542012-12-04 13:59:12 -0600361 VERB("%p:%p: size=0x%08x, flags=%08x", dev, file_priv,
Laurent Pinchartef3f4e92015-12-14 22:39:36 +0200362 args->size.bytes, flags);
363
364 return omap_gem_new_handle(dev, file_priv, args->size, flags,
365 &args->handle);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600366}
367
Rob Clarkcd5351f2011-11-12 12:09:40 -0600368static int ioctl_gem_info(struct drm_device *dev, void *data,
369 struct drm_file *file_priv)
370{
371 struct drm_omap_gem_info *args = data;
372 struct drm_gem_object *obj;
373 int ret = 0;
374
Rob Clarkf5f94542012-12-04 13:59:12 -0600375 VERB("%p:%p: handle=%d", dev, file_priv, args->handle);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600376
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100377 obj = drm_gem_object_lookup(file_priv, args->handle);
YAMANE Toshiakic7f904b2012-11-14 19:30:38 +0900378 if (!obj)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600379 return -ENOENT;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600380
Rob Clarkf7f9f452011-12-05 19:19:22 -0600381 args->size = omap_gem_mmap_size(obj);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600382 args->offset = omap_gem_mmap_offset(obj);
383
384 drm_gem_object_unreference_unlocked(obj);
385
386 return ret;
387}
388
Rob Clarkbaa70942013-08-02 13:27:49 -0400389static const struct drm_ioctl_desc ioctls[DRM_COMMAND_END - DRM_COMMAND_BASE] = {
Hemant Hariyani5f6ab8c2016-06-07 13:23:19 -0500390 DRM_IOCTL_DEF_DRV(OMAP_GET_PARAM, ioctl_get_param,
391 DRM_AUTH | DRM_RENDER_ALLOW),
392 DRM_IOCTL_DEF_DRV(OMAP_SET_PARAM, ioctl_set_param,
393 DRM_AUTH | DRM_MASTER | DRM_ROOT_ONLY),
394 DRM_IOCTL_DEF_DRV(OMAP_GEM_NEW, ioctl_gem_new,
395 DRM_AUTH | DRM_RENDER_ALLOW),
Laurent Pinchartd6f544f2017-05-09 01:27:11 +0300396 /* Deprecated, to be removed. */
397 DRM_IOCTL_DEF_DRV(OMAP_GEM_CPU_PREP, drm_noop,
Hemant Hariyani5f6ab8c2016-06-07 13:23:19 -0500398 DRM_AUTH | DRM_RENDER_ALLOW),
Laurent Pinchartd6f544f2017-05-09 01:27:11 +0300399 /* Deprecated, to be removed. */
400 DRM_IOCTL_DEF_DRV(OMAP_GEM_CPU_FINI, drm_noop,
Hemant Hariyani5f6ab8c2016-06-07 13:23:19 -0500401 DRM_AUTH | DRM_RENDER_ALLOW),
402 DRM_IOCTL_DEF_DRV(OMAP_GEM_INFO, ioctl_gem_info,
403 DRM_AUTH | DRM_RENDER_ALLOW),
Rob Clarkcd5351f2011-11-12 12:09:40 -0600404};
405
406/*
407 * drm driver funcs
408 */
409
Rob Clarkcd5351f2011-11-12 12:09:40 -0600410static int dev_open(struct drm_device *dev, struct drm_file *file)
411{
412 file->driver_priv = NULL;
413
414 DBG("open: dev=%p, file=%p", dev, file);
415
416 return 0;
417}
418
Rob Clarkcd5351f2011-11-12 12:09:40 -0600419/**
420 * lastclose - clean up after all DRM clients have exited
421 * @dev: DRM device
422 *
423 * Take care of cleaning up after all DRM clients have exited. In the
424 * mode setting case, we want to restore the kernel's initial mode (just
425 * in case the last client left us in a bad state).
426 */
427static void dev_lastclose(struct drm_device *dev)
428{
Rob Clark3c810c62012-08-15 15:18:01 -0500429 int i;
430
Lukas Wunnerf15a66e2015-09-05 11:22:39 +0200431 /* we don't support vga_switcheroo.. so just make sure the fbdev
Rob Clarkcd5351f2011-11-12 12:09:40 -0600432 * mode is active
433 */
434 struct omap_drm_private *priv = dev->dev_private;
435 int ret;
436
437 DBG("lastclose: dev=%p", dev);
438
Ville Syrjälä0da88db2016-09-26 19:30:52 +0300439 /* need to restore default rotation state.. not sure
440 * if there is a cleaner way to restore properties to
441 * default state? Maybe a flag that properties should
442 * automatically be restored to default state on
443 * lastclose?
444 */
445 for (i = 0; i < priv->num_crtcs; i++) {
446 struct drm_crtc *crtc = priv->crtcs[i];
Rob Clark3c810c62012-08-15 15:18:01 -0500447
Ville Syrjälä0da88db2016-09-26 19:30:52 +0300448 if (!crtc->primary->rotation_property)
449 continue;
450
451 drm_object_property_set_value(&crtc->base,
452 crtc->primary->rotation_property,
Robert Fossc2c446a2017-05-19 16:50:17 -0400453 DRM_MODE_ROTATE_0);
Ville Syrjälä0da88db2016-09-26 19:30:52 +0300454 }
455
456 for (i = 0; i < priv->num_planes; i++) {
457 struct drm_plane *plane = priv->planes[i];
458
459 if (!plane->rotation_property)
460 continue;
461
462 drm_object_property_set_value(&plane->base,
463 plane->rotation_property,
Robert Fossc2c446a2017-05-19 16:50:17 -0400464 DRM_MODE_ROTATE_0);
Rob Clark3c810c62012-08-15 15:18:01 -0500465 }
466
Tomi Valkeinenc7c1aec2014-09-25 19:24:26 +0000467 if (priv->fbdev) {
468 ret = drm_fb_helper_restore_fbdev_mode_unlocked(priv->fbdev);
469 if (ret)
470 DBG("failed to restore crtc mode");
471 }
Rob Clarkcd5351f2011-11-12 12:09:40 -0600472}
473
Laurent Pinchart78b68552012-05-17 13:27:22 +0200474static const struct vm_operations_struct omap_gem_vm_ops = {
Rob Clarkcd5351f2011-11-12 12:09:40 -0600475 .fault = omap_gem_fault,
476 .open = drm_gem_vm_open,
477 .close = drm_gem_vm_close,
478};
479
Rob Clarkff4f3872012-01-16 12:51:14 -0600480static const struct file_operations omapdriver_fops = {
Laurent Pinchart222025e2015-01-11 00:02:07 +0200481 .owner = THIS_MODULE,
482 .open = drm_open,
483 .unlocked_ioctl = drm_ioctl,
484 .release = drm_release,
485 .mmap = omap_gem_mmap,
486 .poll = drm_poll,
487 .read = drm_read,
488 .llseek = noop_llseek,
Rob Clarkff4f3872012-01-16 12:51:14 -0600489};
490
Rob Clarkcd5351f2011-11-12 12:09:40 -0600491static struct drm_driver omap_drm_driver = {
Tomi Valkeinen728fea72015-10-02 11:10:41 +0300492 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME |
Hemant Hariyani5f6ab8c2016-06-07 13:23:19 -0500493 DRIVER_ATOMIC | DRIVER_RENDER,
Laurent Pinchart222025e2015-01-11 00:02:07 +0200494 .open = dev_open,
495 .lastclose = dev_lastclose,
Andy Gross6169a1482011-12-15 21:05:17 -0600496#ifdef CONFIG_DEBUG_FS
Laurent Pinchart222025e2015-01-11 00:02:07 +0200497 .debugfs_init = omap_debugfs_init,
Andy Gross6169a1482011-12-15 21:05:17 -0600498#endif
Laurent Pinchart222025e2015-01-11 00:02:07 +0200499 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
500 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
501 .gem_prime_export = omap_gem_prime_export,
502 .gem_prime_import = omap_gem_prime_import,
503 .gem_free_object = omap_gem_free_object,
504 .gem_vm_ops = &omap_gem_vm_ops,
505 .dumb_create = omap_gem_dumb_create,
506 .dumb_map_offset = omap_gem_dumb_map_offset,
507 .dumb_destroy = drm_gem_dumb_destroy,
508 .ioctls = ioctls,
509 .num_ioctls = DRM_OMAP_NUM_IOCTLS,
510 .fops = &omapdriver_fops,
511 .name = DRIVER_NAME,
512 .desc = DRIVER_DESC,
513 .date = DRIVER_DATE,
514 .major = DRIVER_MAJOR,
515 .minor = DRIVER_MINOR,
516 .patchlevel = DRIVER_PATCHLEVEL,
Rob Clarkcd5351f2011-11-12 12:09:40 -0600517};
518
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200519static int pdev_probe(struct platform_device *pdev)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600520{
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200521 struct omap_drm_platform_data *pdata = pdev->dev.platform_data;
522 struct omap_drm_private *priv;
523 struct drm_device *ddev;
524 unsigned int i;
525 int ret;
526
527 DBG("%s", pdev->name);
Archit Taneja3a01ab22014-01-02 14:49:51 +0530528
Tomi Valkeinen591a0ac2013-05-23 12:07:50 +0300529 if (omapdss_is_initialized() == false)
530 return -EPROBE_DEFER;
531
Archit Taneja3a01ab22014-01-02 14:49:51 +0530532 omap_crtc_pre_init();
533
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200534 ret = omap_connect_dssdevs();
535 if (ret)
536 goto err_crtc_uninit;
537
538 /* Allocate and initialize the driver private structure. */
539 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
540 if (!priv) {
541 ret = -ENOMEM;
542 goto err_disconnect_dssdevs;
Archit Taneja3a01ab22014-01-02 14:49:51 +0530543 }
544
Tomi Valkeinen9f759222015-11-05 18:39:52 +0200545 priv->dispc_ops = dispc_get_ops();
546
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200547 priv->omaprev = pdata->omaprev;
548 priv->wq = alloc_ordered_workqueue("omapdrm", 0);
549
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200550 spin_lock_init(&priv->list_lock);
551 INIT_LIST_HEAD(&priv->obj_list);
552
553 /* Allocate and initialize the DRM device. */
554 ddev = drm_dev_alloc(&omap_drm_driver, &pdev->dev);
555 if (IS_ERR(ddev)) {
556 ret = PTR_ERR(ddev);
557 goto err_free_priv;
558 }
559
560 ddev->dev_private = priv;
561 platform_set_drvdata(pdev, ddev);
562
563 omap_gem_init(ddev);
564
565 ret = omap_modeset_init(ddev);
566 if (ret) {
567 dev_err(&pdev->dev, "omap_modeset_init failed: ret=%d\n", ret);
568 goto err_free_drm_dev;
569 }
570
571 /* Initialize vblank handling, start with all CRTCs disabled. */
572 ret = drm_vblank_init(ddev, priv->num_crtcs);
573 if (ret) {
574 dev_err(&pdev->dev, "could not init vblank\n");
575 goto err_cleanup_modeset;
576 }
577
578 for (i = 0; i < priv->num_crtcs; i++)
579 drm_crtc_vblank_off(priv->crtcs[i]);
580
581 priv->fbdev = omap_fbdev_init(ddev);
582
583 drm_kms_helper_poll_init(ddev);
584
585 /*
586 * Register the DRM device with the core and the connectors with
587 * sysfs.
588 */
589 ret = drm_dev_register(ddev, 0);
590 if (ret)
591 goto err_cleanup_helpers;
592
593 return 0;
594
595err_cleanup_helpers:
596 drm_kms_helper_poll_fini(ddev);
597 if (priv->fbdev)
598 omap_fbdev_free(ddev);
599err_cleanup_modeset:
600 drm_mode_config_cleanup(ddev);
601 omap_drm_irq_uninstall(ddev);
602err_free_drm_dev:
603 omap_gem_deinit(ddev);
604 drm_dev_unref(ddev);
605err_free_priv:
606 destroy_workqueue(priv->wq);
607 kfree(priv);
608err_disconnect_dssdevs:
609 omap_disconnect_dssdevs();
610err_crtc_uninit:
611 omap_crtc_pre_uninit();
612 return ret;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600613}
614
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200615static int pdev_remove(struct platform_device *pdev)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600616{
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200617 struct drm_device *ddev = platform_get_drvdata(pdev);
618 struct omap_drm_private *priv = ddev->dev_private;
619
Rob Clarkcd5351f2011-11-12 12:09:40 -0600620 DBG("");
Andy Gross5c137792012-03-05 10:48:39 -0600621
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200622 drm_dev_unregister(ddev);
623
624 drm_kms_helper_poll_fini(ddev);
625
626 if (priv->fbdev)
627 omap_fbdev_free(ddev);
628
Tomi Valkeinen8a54aa92017-03-27 10:02:22 +0300629 drm_atomic_helper_shutdown(ddev);
630
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200631 drm_mode_config_cleanup(ddev);
632
633 omap_drm_irq_uninstall(ddev);
634 omap_gem_deinit(ddev);
635
636 drm_dev_unref(ddev);
637
638 destroy_workqueue(priv->wq);
639 kfree(priv);
Tomi Valkeinen707cf582014-04-02 13:47:43 +0300640
Archit Tanejacc823bd2014-01-02 14:49:52 +0530641 omap_disconnect_dssdevs();
642 omap_crtc_pre_uninit();
Daniel Vetterfd3c0252013-12-11 11:34:26 +0100643
Rob Clarkcd5351f2011-11-12 12:09:40 -0600644 return 0;
645}
646
Grygorii Strashko8450c8d2015-02-26 15:57:17 +0200647#ifdef CONFIG_PM_SLEEP
Tomi Valkeinen92bf0f92015-10-02 11:10:42 +0300648static int omap_drm_suspend_all_displays(void)
649{
650 struct omap_dss_device *dssdev = NULL;
651
652 for_each_dss_dev(dssdev) {
653 if (!dssdev->driver)
654 continue;
655
656 if (dssdev->state == OMAP_DSS_DISPLAY_ACTIVE) {
657 dssdev->driver->disable(dssdev);
658 dssdev->activate_after_resume = true;
659 } else {
660 dssdev->activate_after_resume = false;
661 }
662 }
663
664 return 0;
665}
666
667static int omap_drm_resume_all_displays(void)
668{
669 struct omap_dss_device *dssdev = NULL;
670
671 for_each_dss_dev(dssdev) {
672 if (!dssdev->driver)
673 continue;
674
675 if (dssdev->activate_after_resume) {
676 dssdev->driver->enable(dssdev);
677 dssdev->activate_after_resume = false;
678 }
679 }
680
681 return 0;
682}
683
Tomi Valkeinenccd7b5e2014-11-14 15:18:28 +0200684static int omap_drm_suspend(struct device *dev)
685{
686 struct drm_device *drm_dev = dev_get_drvdata(dev);
687
688 drm_kms_helper_poll_disable(drm_dev);
689
Tomi Valkeinen92bf0f92015-10-02 11:10:42 +0300690 drm_modeset_lock_all(drm_dev);
691 omap_drm_suspend_all_displays();
692 drm_modeset_unlock_all(drm_dev);
693
Tomi Valkeinenccd7b5e2014-11-14 15:18:28 +0200694 return 0;
695}
696
697static int omap_drm_resume(struct device *dev)
698{
699 struct drm_device *drm_dev = dev_get_drvdata(dev);
700
Tomi Valkeinen92bf0f92015-10-02 11:10:42 +0300701 drm_modeset_lock_all(drm_dev);
702 omap_drm_resume_all_displays();
703 drm_modeset_unlock_all(drm_dev);
704
Tomi Valkeinenccd7b5e2014-11-14 15:18:28 +0200705 drm_kms_helper_poll_enable(drm_dev);
706
707 return omap_gem_resume(dev);
708}
Andy Grosse78edba2012-12-19 14:53:37 -0600709#endif
710
Grygorii Strashko8450c8d2015-02-26 15:57:17 +0200711static SIMPLE_DEV_PM_OPS(omapdrm_pm_ops, omap_drm_suspend, omap_drm_resume);
712
Tomi Valkeinen6717cd22013-04-10 10:44:00 +0300713static struct platform_driver pdev = {
Laurent Pinchart222025e2015-01-11 00:02:07 +0200714 .driver = {
715 .name = DRIVER_NAME,
Laurent Pinchart222025e2015-01-11 00:02:07 +0200716 .pm = &omapdrm_pm_ops,
Laurent Pinchart222025e2015-01-11 00:02:07 +0200717 },
718 .probe = pdev_probe,
719 .remove = pdev_remove,
Rob Clarkcd5351f2011-11-12 12:09:40 -0600720};
721
Thierry Redinge1c49bd2015-12-02 17:23:31 +0100722static struct platform_driver * const drivers[] = {
723 &omap_dmm_driver,
724 &pdev,
725};
726
Rob Clarkcd5351f2011-11-12 12:09:40 -0600727static int __init omap_drm_init(void)
728{
729 DBG("init");
Tomi Valkeinenea7e3a62014-04-02 14:31:50 +0300730
Thierry Redinge1c49bd2015-12-02 17:23:31 +0100731 return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
Rob Clarkcd5351f2011-11-12 12:09:40 -0600732}
733
734static void __exit omap_drm_fini(void)
735{
736 DBG("fini");
Tomi Valkeinenea7e3a62014-04-02 14:31:50 +0300737
Thierry Redinge1c49bd2015-12-02 17:23:31 +0100738 platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
Rob Clarkcd5351f2011-11-12 12:09:40 -0600739}
740
741/* need late_initcall() so we load after dss_driver's are loaded */
742late_initcall(omap_drm_init);
743module_exit(omap_drm_fini);
744
745MODULE_AUTHOR("Rob Clark <rob@ti.com>");
746MODULE_DESCRIPTION("OMAP DRM Display Driver");
747MODULE_ALIAS("platform:" DRIVER_NAME);
748MODULE_LICENSE("GPL v2");