blob: 51e0c39587960c3cfbdcd8974e2351574b41dc6e [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{
60 struct drm_crtc_state *old_crtc_state;
61 struct drm_crtc *crtc;
62 unsigned int i;
63 int ret;
64
65 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
66 if (!crtc->state->enable)
67 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
87 /* With the current dss dispc implementation we have to enable
88 * the new modeset before we can commit planes. The dispc ovl
89 * configuration relies on the video mode configuration been
90 * written into the HW when the ovl configuration is
91 * calculated.
92 *
93 * This approach is not ideal because after a mode change the
94 * plane update is executed only after the first vblank
95 * interrupt. The dispc implementation should be fixed so that
96 * it is able use uncommitted drm state information.
97 */
Laurent Pinchart748471a52015-03-05 23:42:39 +020098 drm_atomic_helper_commit_modeset_enables(dev, old_state);
Jyri Sarha897145d2017-01-27 12:04:55 +020099 omap_atomic_wait_for_completion(dev, old_state);
100
101 drm_atomic_helper_commit_planes(dev, old_state, 0);
Laurent Pinchart748471a52015-03-05 23:42:39 +0200102
Laurent Pincharta9e6f9f2017-05-09 01:27:10 +0300103 drm_atomic_helper_commit_hw_done(old_state);
104
105 /*
106 * Wait for completion of the page flips to ensure that old buffers
107 * can't be touched by the hardware anymore before cleaning up planes.
108 */
Tomi Valkeinen5f741b32015-05-29 16:01:18 +0300109 omap_atomic_wait_for_completion(dev, old_state);
Laurent Pinchart748471a52015-03-05 23:42:39 +0200110
111 drm_atomic_helper_cleanup_planes(dev, old_state);
112
Tomi Valkeinen9f759222015-11-05 18:39:52 +0200113 priv->dispc_ops->runtime_put();
Laurent Pinchart748471a52015-03-05 23:42:39 +0200114}
115
Laurent Pincharta9e6f9f2017-05-09 01:27:10 +0300116static const struct drm_mode_config_helper_funcs omap_mode_config_helper_funcs = {
117 .atomic_commit_tail = omap_atomic_commit_tail,
118};
Laurent Pinchart748471a52015-03-05 23:42:39 +0200119
Laurent Pincharte6ecefa2012-05-17 13:27:23 +0200120static const struct drm_mode_config_funcs omap_mode_config_funcs = {
Rob Clarkcd5351f2011-11-12 12:09:40 -0600121 .fb_create = omap_framebuffer_create,
122 .output_poll_changed = omap_fb_output_poll_changed,
Laurent Pinchartcef77d42015-03-05 21:50:00 +0200123 .atomic_check = drm_atomic_helper_check,
Laurent Pincharta9e6f9f2017-05-09 01:27:10 +0300124 .atomic_commit = drm_atomic_helper_commit,
Rob Clarkcd5351f2011-11-12 12:09:40 -0600125};
126
127static int get_connector_type(struct omap_dss_device *dssdev)
128{
129 switch (dssdev->type) {
130 case OMAP_DISPLAY_TYPE_HDMI:
131 return DRM_MODE_CONNECTOR_HDMIA;
Tomi Valkeinen4635c172013-05-14 14:14:15 +0300132 case OMAP_DISPLAY_TYPE_DVI:
133 return DRM_MODE_CONNECTOR_DVID;
Sebastian Reichel4a64b902016-03-08 17:39:36 +0100134 case OMAP_DISPLAY_TYPE_DSI:
135 return DRM_MODE_CONNECTOR_DSI;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600136 default:
137 return DRM_MODE_CONNECTOR_Unknown;
138 }
139}
140
Archit Tanejacc823bd2014-01-02 14:49:52 +0530141static void omap_disconnect_dssdevs(void)
142{
143 struct omap_dss_device *dssdev = NULL;
144
145 for_each_dss_dev(dssdev)
146 dssdev->driver->disconnect(dssdev);
147}
Archit Taneja0d8f3712013-03-26 19:15:19 +0530148
Archit Taneja3a01ab22014-01-02 14:49:51 +0530149static int omap_connect_dssdevs(void)
150{
151 int r;
152 struct omap_dss_device *dssdev = NULL;
Peter Ujfalusia09d2bc2016-05-03 22:08:01 +0300153
154 if (!omapdss_stack_is_ready())
155 return -EPROBE_DEFER;
Archit Taneja3a01ab22014-01-02 14:49:51 +0530156
157 for_each_dss_dev(dssdev) {
158 r = dssdev->driver->connect(dssdev);
159 if (r == -EPROBE_DEFER) {
160 omap_dss_put_device(dssdev);
161 goto cleanup;
162 } else if (r) {
163 dev_warn(dssdev->dev, "could not connect display: %s\n",
164 dssdev->name);
Archit Taneja3a01ab22014-01-02 14:49:51 +0530165 }
166 }
167
Archit Taneja3a01ab22014-01-02 14:49:51 +0530168 return 0;
169
170cleanup:
171 /*
172 * if we are deferring probe, we disconnect the devices we previously
173 * connected
174 */
Archit Tanejacc823bd2014-01-02 14:49:52 +0530175 omap_disconnect_dssdevs();
Archit Taneja3a01ab22014-01-02 14:49:51 +0530176
177 return r;
178}
Rob Clarkcd5351f2011-11-12 12:09:40 -0600179
Laurent Pincharte2cd09b2015-03-06 17:16:43 +0200180static int omap_modeset_init_properties(struct drm_device *dev)
181{
182 struct omap_drm_private *priv = dev->dev_private;
Laurent Pinchartdff6c242017-05-09 01:27:14 +0300183 unsigned int num_planes = priv->dispc_ops->get_num_ovls();
Laurent Pincharte2cd09b2015-03-06 17:16:43 +0200184
Laurent Pinchartdff6c242017-05-09 01:27:14 +0300185 priv->zorder_prop = drm_property_create_range(dev, 0, "zorder", 0,
186 num_planes - 1);
Laurent Pincharte2cd09b2015-03-06 17:16:43 +0200187 if (!priv->zorder_prop)
188 return -ENOMEM;
189
190 return 0;
191}
192
Rob Clarkcd5351f2011-11-12 12:09:40 -0600193static int omap_modeset_init(struct drm_device *dev)
194{
Rob Clarkcd5351f2011-11-12 12:09:40 -0600195 struct omap_drm_private *priv = dev->dev_private;
196 struct omap_dss_device *dssdev = NULL;
Tomi Valkeinen9f759222015-11-05 18:39:52 +0200197 int num_ovls = priv->dispc_ops->get_num_ovls();
198 int num_mgrs = priv->dispc_ops->get_num_mgrs();
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200199 int num_crtcs, crtc_idx, plane_idx;
Laurent Pinchartfb9a35f2015-01-11 16:30:44 +0200200 int ret;
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200201 u32 plane_crtc_mask;
Tomi Valkeinen04b1fc02013-05-14 10:55:19 +0300202
Rob Clarkcd5351f2011-11-12 12:09:40 -0600203 drm_mode_config_init(dev);
204
Laurent Pincharte2cd09b2015-03-06 17:16:43 +0200205 ret = omap_modeset_init_properties(dev);
206 if (ret < 0)
207 return ret;
208
Rob Clarkf5f94542012-12-04 13:59:12 -0600209 /*
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200210 * This function creates exactly one connector, encoder, crtc,
211 * and primary plane per each connected dss-device. Each
212 * connector->encoder->crtc chain is expected to be separate
213 * and each crtc is connect to a single dss-channel. If the
214 * configuration does not match the expectations or exceeds
215 * the available resources, the configuration is rejected.
Rob Clarkf5f94542012-12-04 13:59:12 -0600216 */
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200217 num_crtcs = 0;
Jyri Sarhaf1118b82017-03-24 16:47:51 +0200218 for_each_dss_dev(dssdev)
219 if (omapdss_device_is_connected(dssdev))
220 num_crtcs++;
221
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200222 if (num_crtcs > num_mgrs || num_crtcs > num_ovls ||
223 num_crtcs > ARRAY_SIZE(priv->crtcs) ||
224 num_crtcs > ARRAY_SIZE(priv->planes) ||
225 num_crtcs > ARRAY_SIZE(priv->encoders) ||
226 num_crtcs > ARRAY_SIZE(priv->connectors)) {
227 dev_err(dev->dev, "%s(): Too many connected displays\n",
228 __func__);
229 return -EINVAL;
230 }
231
232 /* All planes can be put to any CRTC */
233 plane_crtc_mask = (1 << num_crtcs) - 1;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600234
Archit Taneja0d8f3712013-03-26 19:15:19 +0530235 dssdev = NULL;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600236
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200237 crtc_idx = 0;
238 plane_idx = 0;
Rob Clarkf5f94542012-12-04 13:59:12 -0600239 for_each_dss_dev(dssdev) {
240 struct drm_connector *connector;
241 struct drm_encoder *encoder;
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200242 struct drm_plane *plane;
243 struct drm_crtc *crtc;
Rob Clarkf5f94542012-12-04 13:59:12 -0600244
Archit Taneja3a01ab22014-01-02 14:49:51 +0530245 if (!omapdss_device_is_connected(dssdev))
Archit Taneja581382e2013-03-26 19:15:18 +0530246 continue;
Tomi Valkeinena7e71e72013-05-08 16:23:32 +0300247
Rob Clarkf5f94542012-12-04 13:59:12 -0600248 encoder = omap_encoder_init(dev, dssdev);
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200249 if (!encoder)
Rob Clarkf5f94542012-12-04 13:59:12 -0600250 return -ENOMEM;
Rob Clarkf5f94542012-12-04 13:59:12 -0600251
252 connector = omap_connector_init(dev,
253 get_connector_type(dssdev), dssdev, encoder);
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200254 if (!connector)
Rob Clarkf5f94542012-12-04 13:59:12 -0600255 return -ENOMEM;
Rob Clarkf5f94542012-12-04 13:59:12 -0600256
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200257 plane = omap_plane_init(dev, plane_idx, DRM_PLANE_TYPE_PRIMARY,
258 plane_crtc_mask);
259 if (IS_ERR(plane))
260 return PTR_ERR(plane);
Rob Clarkf5f94542012-12-04 13:59:12 -0600261
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200262 crtc = omap_crtc_init(dev, plane, dssdev);
263 if (IS_ERR(crtc))
264 return PTR_ERR(crtc);
265
266 drm_mode_connector_attach_encoder(connector, encoder);
267 encoder->possible_crtcs = (1 << crtc_idx);
268
269 priv->crtcs[priv->num_crtcs++] = crtc;
270 priv->planes[priv->num_planes++] = plane;
Rob Clarkf5f94542012-12-04 13:59:12 -0600271 priv->encoders[priv->num_encoders++] = encoder;
272 priv->connectors[priv->num_connectors++] = connector;
273
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200274 plane_idx++;
275 crtc_idx++;
Archit Taneja0d8f3712013-03-26 19:15:19 +0530276 }
277
278 /*
Archit Taneja0d8f3712013-03-26 19:15:19 +0530279 * Create normal planes for the remaining overlays:
280 */
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200281 for (; plane_idx < num_ovls; plane_idx++) {
Laurent Pinchartfb9a35f2015-01-11 16:30:44 +0200282 struct drm_plane *plane;
283
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200284 if (WARN_ON(priv->num_planes >= ARRAY_SIZE(priv->planes)))
285 return -EINVAL;
286
287 plane = omap_plane_init(dev, plane_idx, DRM_PLANE_TYPE_OVERLAY,
288 plane_crtc_mask);
Laurent Pinchartfb9a35f2015-01-11 16:30:44 +0200289 if (IS_ERR(plane))
290 return PTR_ERR(plane);
Archit Taneja0d8f3712013-03-26 19:15:19 +0530291
Archit Taneja0d8f3712013-03-26 19:15:19 +0530292 priv->planes[priv->num_planes++] = plane;
293 }
294
Archit Taneja0d8f3712013-03-26 19:15:19 +0530295 DBG("registered %d planes, %d crtcs, %d encoders and %d connectors\n",
296 priv->num_planes, priv->num_crtcs, priv->num_encoders,
297 priv->num_connectors);
298
Tomi Valkeinen1e907112016-08-23 12:35:39 +0300299 dev->mode_config.min_width = 8;
300 dev->mode_config.min_height = 2;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600301
302 /* note: eventually will need some cpu_is_omapXYZ() type stuff here
303 * to fill in these limits properly on different OMAP generations..
304 */
305 dev->mode_config.max_width = 2048;
306 dev->mode_config.max_height = 2048;
307
308 dev->mode_config.funcs = &omap_mode_config_funcs;
Laurent Pincharta9e6f9f2017-05-09 01:27:10 +0300309 dev->mode_config.helper_private = &omap_mode_config_helper_funcs;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600310
Laurent Pinchart69a12262015-03-05 21:38:16 +0200311 drm_mode_config_reset(dev);
312
Laurent Pinchart728ae8d2015-05-28 00:21:29 +0300313 omap_drm_irq_install(dev);
314
Rob Clarkcd5351f2011-11-12 12:09:40 -0600315 return 0;
316}
317
Rob Clarkcd5351f2011-11-12 12:09:40 -0600318/*
319 * drm ioctl funcs
320 */
321
322
323static int ioctl_get_param(struct drm_device *dev, void *data,
324 struct drm_file *file_priv)
325{
Rob Clark5e3b0872012-10-29 09:31:12 +0100326 struct omap_drm_private *priv = dev->dev_private;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600327 struct drm_omap_param *args = data;
328
329 DBG("%p: param=%llu", dev, args->param);
330
331 switch (args->param) {
332 case OMAP_PARAM_CHIPSET_ID:
Rob Clark5e3b0872012-10-29 09:31:12 +0100333 args->value = priv->omaprev;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600334 break;
335 default:
336 DBG("unknown parameter %lld", args->param);
337 return -EINVAL;
338 }
339
340 return 0;
341}
342
343static int ioctl_set_param(struct drm_device *dev, void *data,
344 struct drm_file *file_priv)
345{
346 struct drm_omap_param *args = data;
347
348 switch (args->param) {
349 default:
350 DBG("unknown parameter %lld", args->param);
351 return -EINVAL;
352 }
353
354 return 0;
355}
356
Laurent Pinchartef3f4e92015-12-14 22:39:36 +0200357#define OMAP_BO_USER_MASK 0x00ffffff /* flags settable by userspace */
358
Rob Clarkcd5351f2011-11-12 12:09:40 -0600359static int ioctl_gem_new(struct drm_device *dev, void *data,
360 struct drm_file *file_priv)
361{
362 struct drm_omap_gem_new *args = data;
Laurent Pinchartef3f4e92015-12-14 22:39:36 +0200363 u32 flags = args->flags & OMAP_BO_USER_MASK;
364
Rob Clarkf5f94542012-12-04 13:59:12 -0600365 VERB("%p:%p: size=0x%08x, flags=%08x", dev, file_priv,
Laurent Pinchartef3f4e92015-12-14 22:39:36 +0200366 args->size.bytes, flags);
367
368 return omap_gem_new_handle(dev, file_priv, args->size, flags,
369 &args->handle);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600370}
371
Rob Clarkcd5351f2011-11-12 12:09:40 -0600372static int ioctl_gem_info(struct drm_device *dev, void *data,
373 struct drm_file *file_priv)
374{
375 struct drm_omap_gem_info *args = data;
376 struct drm_gem_object *obj;
377 int ret = 0;
378
Rob Clarkf5f94542012-12-04 13:59:12 -0600379 VERB("%p:%p: handle=%d", dev, file_priv, args->handle);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600380
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100381 obj = drm_gem_object_lookup(file_priv, args->handle);
YAMANE Toshiakic7f904b2012-11-14 19:30:38 +0900382 if (!obj)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600383 return -ENOENT;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600384
Rob Clarkf7f9f452011-12-05 19:19:22 -0600385 args->size = omap_gem_mmap_size(obj);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600386 args->offset = omap_gem_mmap_offset(obj);
387
388 drm_gem_object_unreference_unlocked(obj);
389
390 return ret;
391}
392
Rob Clarkbaa70942013-08-02 13:27:49 -0400393static const struct drm_ioctl_desc ioctls[DRM_COMMAND_END - DRM_COMMAND_BASE] = {
Hemant Hariyani5f6ab8c2016-06-07 13:23:19 -0500394 DRM_IOCTL_DEF_DRV(OMAP_GET_PARAM, ioctl_get_param,
395 DRM_AUTH | DRM_RENDER_ALLOW),
396 DRM_IOCTL_DEF_DRV(OMAP_SET_PARAM, ioctl_set_param,
397 DRM_AUTH | DRM_MASTER | DRM_ROOT_ONLY),
398 DRM_IOCTL_DEF_DRV(OMAP_GEM_NEW, ioctl_gem_new,
399 DRM_AUTH | DRM_RENDER_ALLOW),
Laurent Pinchartd6f544f2017-05-09 01:27:11 +0300400 /* Deprecated, to be removed. */
401 DRM_IOCTL_DEF_DRV(OMAP_GEM_CPU_PREP, drm_noop,
Hemant Hariyani5f6ab8c2016-06-07 13:23:19 -0500402 DRM_AUTH | DRM_RENDER_ALLOW),
Laurent Pinchartd6f544f2017-05-09 01:27:11 +0300403 /* Deprecated, to be removed. */
404 DRM_IOCTL_DEF_DRV(OMAP_GEM_CPU_FINI, drm_noop,
Hemant Hariyani5f6ab8c2016-06-07 13:23:19 -0500405 DRM_AUTH | DRM_RENDER_ALLOW),
406 DRM_IOCTL_DEF_DRV(OMAP_GEM_INFO, ioctl_gem_info,
407 DRM_AUTH | DRM_RENDER_ALLOW),
Rob Clarkcd5351f2011-11-12 12:09:40 -0600408};
409
410/*
411 * drm driver funcs
412 */
413
Rob Clarkcd5351f2011-11-12 12:09:40 -0600414static int dev_open(struct drm_device *dev, struct drm_file *file)
415{
416 file->driver_priv = NULL;
417
418 DBG("open: dev=%p, file=%p", dev, file);
419
420 return 0;
421}
422
Rob Clarkcd5351f2011-11-12 12:09:40 -0600423/**
424 * lastclose - clean up after all DRM clients have exited
425 * @dev: DRM device
426 *
427 * Take care of cleaning up after all DRM clients have exited. In the
428 * mode setting case, we want to restore the kernel's initial mode (just
429 * in case the last client left us in a bad state).
430 */
431static void dev_lastclose(struct drm_device *dev)
432{
Rob Clark3c810c62012-08-15 15:18:01 -0500433 int i;
434
Lukas Wunnerf15a66e2015-09-05 11:22:39 +0200435 /* we don't support vga_switcheroo.. so just make sure the fbdev
Rob Clarkcd5351f2011-11-12 12:09:40 -0600436 * mode is active
437 */
438 struct omap_drm_private *priv = dev->dev_private;
439 int ret;
440
441 DBG("lastclose: dev=%p", dev);
442
Ville Syrjälä0da88db2016-09-26 19:30:52 +0300443 /* need to restore default rotation state.. not sure
444 * if there is a cleaner way to restore properties to
445 * default state? Maybe a flag that properties should
446 * automatically be restored to default state on
447 * lastclose?
448 */
449 for (i = 0; i < priv->num_crtcs; i++) {
450 struct drm_crtc *crtc = priv->crtcs[i];
Rob Clark3c810c62012-08-15 15:18:01 -0500451
Ville Syrjälä0da88db2016-09-26 19:30:52 +0300452 if (!crtc->primary->rotation_property)
453 continue;
454
455 drm_object_property_set_value(&crtc->base,
456 crtc->primary->rotation_property,
Robert Fossc2c446a2017-05-19 16:50:17 -0400457 DRM_MODE_ROTATE_0);
Ville Syrjälä0da88db2016-09-26 19:30:52 +0300458 }
459
460 for (i = 0; i < priv->num_planes; i++) {
461 struct drm_plane *plane = priv->planes[i];
462
463 if (!plane->rotation_property)
464 continue;
465
466 drm_object_property_set_value(&plane->base,
467 plane->rotation_property,
Robert Fossc2c446a2017-05-19 16:50:17 -0400468 DRM_MODE_ROTATE_0);
Rob Clark3c810c62012-08-15 15:18:01 -0500469 }
470
Tomi Valkeinenc7c1aec2014-09-25 19:24:26 +0000471 if (priv->fbdev) {
472 ret = drm_fb_helper_restore_fbdev_mode_unlocked(priv->fbdev);
473 if (ret)
474 DBG("failed to restore crtc mode");
475 }
Rob Clarkcd5351f2011-11-12 12:09:40 -0600476}
477
Laurent Pinchart78b68552012-05-17 13:27:22 +0200478static const struct vm_operations_struct omap_gem_vm_ops = {
Rob Clarkcd5351f2011-11-12 12:09:40 -0600479 .fault = omap_gem_fault,
480 .open = drm_gem_vm_open,
481 .close = drm_gem_vm_close,
482};
483
Rob Clarkff4f3872012-01-16 12:51:14 -0600484static const struct file_operations omapdriver_fops = {
Laurent Pinchart222025e2015-01-11 00:02:07 +0200485 .owner = THIS_MODULE,
486 .open = drm_open,
487 .unlocked_ioctl = drm_ioctl,
488 .release = drm_release,
489 .mmap = omap_gem_mmap,
490 .poll = drm_poll,
491 .read = drm_read,
492 .llseek = noop_llseek,
Rob Clarkff4f3872012-01-16 12:51:14 -0600493};
494
Rob Clarkcd5351f2011-11-12 12:09:40 -0600495static struct drm_driver omap_drm_driver = {
Tomi Valkeinen728fea72015-10-02 11:10:41 +0300496 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME |
Hemant Hariyani5f6ab8c2016-06-07 13:23:19 -0500497 DRIVER_ATOMIC | DRIVER_RENDER,
Laurent Pinchart222025e2015-01-11 00:02:07 +0200498 .open = dev_open,
499 .lastclose = dev_lastclose,
Andy Gross6169a1482011-12-15 21:05:17 -0600500#ifdef CONFIG_DEBUG_FS
Laurent Pinchart222025e2015-01-11 00:02:07 +0200501 .debugfs_init = omap_debugfs_init,
Andy Gross6169a1482011-12-15 21:05:17 -0600502#endif
Laurent Pinchart222025e2015-01-11 00:02:07 +0200503 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
504 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
505 .gem_prime_export = omap_gem_prime_export,
506 .gem_prime_import = omap_gem_prime_import,
507 .gem_free_object = omap_gem_free_object,
508 .gem_vm_ops = &omap_gem_vm_ops,
509 .dumb_create = omap_gem_dumb_create,
510 .dumb_map_offset = omap_gem_dumb_map_offset,
511 .dumb_destroy = drm_gem_dumb_destroy,
512 .ioctls = ioctls,
513 .num_ioctls = DRM_OMAP_NUM_IOCTLS,
514 .fops = &omapdriver_fops,
515 .name = DRIVER_NAME,
516 .desc = DRIVER_DESC,
517 .date = DRIVER_DATE,
518 .major = DRIVER_MAJOR,
519 .minor = DRIVER_MINOR,
520 .patchlevel = DRIVER_PATCHLEVEL,
Rob Clarkcd5351f2011-11-12 12:09:40 -0600521};
522
Laurent Pinchart6e471fa2017-05-06 02:57:12 +0300523static const struct soc_device_attribute omapdrm_soc_devices[] = {
524 { .family = "OMAP3", .data = (void *)0x3430 },
525 { .family = "OMAP4", .data = (void *)0x4430 },
526 { .family = "OMAP5", .data = (void *)0x5430 },
527 { .family = "DRA7", .data = (void *)0x0752 },
528 { /* sentinel */ }
529};
530
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200531static int pdev_probe(struct platform_device *pdev)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600532{
Laurent Pinchart6e471fa2017-05-06 02:57:12 +0300533 const struct soc_device_attribute *soc;
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200534 struct omap_drm_private *priv;
535 struct drm_device *ddev;
536 unsigned int i;
537 int ret;
538
539 DBG("%s", pdev->name);
Archit Taneja3a01ab22014-01-02 14:49:51 +0530540
Tomi Valkeinen591a0ac2013-05-23 12:07:50 +0300541 if (omapdss_is_initialized() == false)
542 return -EPROBE_DEFER;
543
Archit Taneja3a01ab22014-01-02 14:49:51 +0530544 omap_crtc_pre_init();
545
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200546 ret = omap_connect_dssdevs();
547 if (ret)
548 goto err_crtc_uninit;
549
550 /* Allocate and initialize the driver private structure. */
551 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
552 if (!priv) {
553 ret = -ENOMEM;
554 goto err_disconnect_dssdevs;
Archit Taneja3a01ab22014-01-02 14:49:51 +0530555 }
556
Tomi Valkeinen9f759222015-11-05 18:39:52 +0200557 priv->dispc_ops = dispc_get_ops();
558
Laurent Pinchart6e471fa2017-05-06 02:57:12 +0300559 soc = soc_device_match(omapdrm_soc_devices);
560 priv->omaprev = soc ? (unsigned int)soc->data : 0;
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200561 priv->wq = alloc_ordered_workqueue("omapdrm", 0);
562
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200563 spin_lock_init(&priv->list_lock);
564 INIT_LIST_HEAD(&priv->obj_list);
565
566 /* Allocate and initialize the DRM device. */
567 ddev = drm_dev_alloc(&omap_drm_driver, &pdev->dev);
568 if (IS_ERR(ddev)) {
569 ret = PTR_ERR(ddev);
570 goto err_free_priv;
571 }
572
573 ddev->dev_private = priv;
574 platform_set_drvdata(pdev, ddev);
575
576 omap_gem_init(ddev);
577
578 ret = omap_modeset_init(ddev);
579 if (ret) {
580 dev_err(&pdev->dev, "omap_modeset_init failed: ret=%d\n", ret);
581 goto err_free_drm_dev;
582 }
583
584 /* Initialize vblank handling, start with all CRTCs disabled. */
585 ret = drm_vblank_init(ddev, priv->num_crtcs);
586 if (ret) {
587 dev_err(&pdev->dev, "could not init vblank\n");
588 goto err_cleanup_modeset;
589 }
590
591 for (i = 0; i < priv->num_crtcs; i++)
592 drm_crtc_vblank_off(priv->crtcs[i]);
593
594 priv->fbdev = omap_fbdev_init(ddev);
595
596 drm_kms_helper_poll_init(ddev);
597
598 /*
599 * Register the DRM device with the core and the connectors with
600 * sysfs.
601 */
602 ret = drm_dev_register(ddev, 0);
603 if (ret)
604 goto err_cleanup_helpers;
605
606 return 0;
607
608err_cleanup_helpers:
609 drm_kms_helper_poll_fini(ddev);
610 if (priv->fbdev)
611 omap_fbdev_free(ddev);
612err_cleanup_modeset:
613 drm_mode_config_cleanup(ddev);
614 omap_drm_irq_uninstall(ddev);
615err_free_drm_dev:
616 omap_gem_deinit(ddev);
617 drm_dev_unref(ddev);
618err_free_priv:
619 destroy_workqueue(priv->wq);
620 kfree(priv);
621err_disconnect_dssdevs:
622 omap_disconnect_dssdevs();
623err_crtc_uninit:
624 omap_crtc_pre_uninit();
625 return ret;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600626}
627
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200628static int pdev_remove(struct platform_device *pdev)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600629{
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200630 struct drm_device *ddev = platform_get_drvdata(pdev);
631 struct omap_drm_private *priv = ddev->dev_private;
632
Rob Clarkcd5351f2011-11-12 12:09:40 -0600633 DBG("");
Andy Gross5c137792012-03-05 10:48:39 -0600634
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200635 drm_dev_unregister(ddev);
636
637 drm_kms_helper_poll_fini(ddev);
638
639 if (priv->fbdev)
640 omap_fbdev_free(ddev);
641
Tomi Valkeinen8a54aa92017-03-27 10:02:22 +0300642 drm_atomic_helper_shutdown(ddev);
643
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200644 drm_mode_config_cleanup(ddev);
645
646 omap_drm_irq_uninstall(ddev);
647 omap_gem_deinit(ddev);
648
649 drm_dev_unref(ddev);
650
651 destroy_workqueue(priv->wq);
652 kfree(priv);
Tomi Valkeinen707cf582014-04-02 13:47:43 +0300653
Archit Tanejacc823bd2014-01-02 14:49:52 +0530654 omap_disconnect_dssdevs();
655 omap_crtc_pre_uninit();
Daniel Vetterfd3c0252013-12-11 11:34:26 +0100656
Rob Clarkcd5351f2011-11-12 12:09:40 -0600657 return 0;
658}
659
Grygorii Strashko8450c8d2015-02-26 15:57:17 +0200660#ifdef CONFIG_PM_SLEEP
Tomi Valkeinen92bf0f92015-10-02 11:10:42 +0300661static int omap_drm_suspend_all_displays(void)
662{
663 struct omap_dss_device *dssdev = NULL;
664
665 for_each_dss_dev(dssdev) {
666 if (!dssdev->driver)
667 continue;
668
669 if (dssdev->state == OMAP_DSS_DISPLAY_ACTIVE) {
670 dssdev->driver->disable(dssdev);
671 dssdev->activate_after_resume = true;
672 } else {
673 dssdev->activate_after_resume = false;
674 }
675 }
676
677 return 0;
678}
679
680static int omap_drm_resume_all_displays(void)
681{
682 struct omap_dss_device *dssdev = NULL;
683
684 for_each_dss_dev(dssdev) {
685 if (!dssdev->driver)
686 continue;
687
688 if (dssdev->activate_after_resume) {
689 dssdev->driver->enable(dssdev);
690 dssdev->activate_after_resume = false;
691 }
692 }
693
694 return 0;
695}
696
Tomi Valkeinenccd7b5e2014-11-14 15:18:28 +0200697static int omap_drm_suspend(struct device *dev)
698{
699 struct drm_device *drm_dev = dev_get_drvdata(dev);
700
701 drm_kms_helper_poll_disable(drm_dev);
702
Tomi Valkeinen92bf0f92015-10-02 11:10:42 +0300703 drm_modeset_lock_all(drm_dev);
704 omap_drm_suspend_all_displays();
705 drm_modeset_unlock_all(drm_dev);
706
Tomi Valkeinenccd7b5e2014-11-14 15:18:28 +0200707 return 0;
708}
709
710static int omap_drm_resume(struct device *dev)
711{
712 struct drm_device *drm_dev = dev_get_drvdata(dev);
713
Tomi Valkeinen92bf0f92015-10-02 11:10:42 +0300714 drm_modeset_lock_all(drm_dev);
715 omap_drm_resume_all_displays();
716 drm_modeset_unlock_all(drm_dev);
717
Tomi Valkeinenccd7b5e2014-11-14 15:18:28 +0200718 drm_kms_helper_poll_enable(drm_dev);
719
720 return omap_gem_resume(dev);
721}
Andy Grosse78edba2012-12-19 14:53:37 -0600722#endif
723
Grygorii Strashko8450c8d2015-02-26 15:57:17 +0200724static SIMPLE_DEV_PM_OPS(omapdrm_pm_ops, omap_drm_suspend, omap_drm_resume);
725
Tomi Valkeinen6717cd22013-04-10 10:44:00 +0300726static struct platform_driver pdev = {
Laurent Pinchart222025e2015-01-11 00:02:07 +0200727 .driver = {
728 .name = DRIVER_NAME,
Laurent Pinchart222025e2015-01-11 00:02:07 +0200729 .pm = &omapdrm_pm_ops,
Laurent Pinchart222025e2015-01-11 00:02:07 +0200730 },
731 .probe = pdev_probe,
732 .remove = pdev_remove,
Rob Clarkcd5351f2011-11-12 12:09:40 -0600733};
734
Thierry Redinge1c49bd2015-12-02 17:23:31 +0100735static struct platform_driver * const drivers[] = {
736 &omap_dmm_driver,
737 &pdev,
738};
739
Rob Clarkcd5351f2011-11-12 12:09:40 -0600740static int __init omap_drm_init(void)
741{
742 DBG("init");
Tomi Valkeinenea7e3a62014-04-02 14:31:50 +0300743
Thierry Redinge1c49bd2015-12-02 17:23:31 +0100744 return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
Rob Clarkcd5351f2011-11-12 12:09:40 -0600745}
746
747static void __exit omap_drm_fini(void)
748{
749 DBG("fini");
Tomi Valkeinenea7e3a62014-04-02 14:31:50 +0300750
Thierry Redinge1c49bd2015-12-02 17:23:31 +0100751 platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
Rob Clarkcd5351f2011-11-12 12:09:40 -0600752}
753
754/* need late_initcall() so we load after dss_driver's are loaded */
755late_initcall(omap_drm_init);
756module_exit(omap_drm_fini);
757
758MODULE_AUTHOR("Rob Clark <rob@ti.com>");
759MODULE_DESCRIPTION("OMAP DRM Display Driver");
760MODULE_ALIAS("platform:" DRIVER_NAME);
761MODULE_LICENSE("GPL v2");