blob: 7b917c0c1a27683756e28c619d0e146cb7cad1e7 [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 <linux/wait.h>
21
22#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 -060037static int num_crtc = CONFIG_DRM_OMAP_NUM_CRTCS;
38
39MODULE_PARM_DESC(num_crtc, "Number of overlays to use as CRTCs");
40module_param(num_crtc, int, 0600);
41
42/*
43 * mode config funcs
44 */
45
46/* Notes about mapping DSS and DRM entities:
47 * CRTC: overlay
48 * encoder: manager.. with some extension to allow one primary CRTC
49 * and zero or more video CRTC's to be mapped to one encoder?
50 * connector: dssdev.. manager can be attached/detached from different
51 * devices
52 */
53
54static void omap_fb_output_poll_changed(struct drm_device *dev)
55{
56 struct omap_drm_private *priv = dev->dev_private;
57 DBG("dev=%p", dev);
YAMANE Toshiakic7f904b2012-11-14 19:30:38 +090058 if (priv->fbdev)
Rob Clarkcd5351f2011-11-12 12:09:40 -060059 drm_fb_helper_hotplug_event(priv->fbdev);
Rob Clarkcd5351f2011-11-12 12:09:40 -060060}
61
Laurent Pinchart748471a52015-03-05 23:42:39 +020062struct omap_atomic_state_commit {
63 struct work_struct work;
64 struct drm_device *dev;
65 struct drm_atomic_state *state;
66 u32 crtcs;
67};
68
Tomi Valkeinen5f741b32015-05-29 16:01:18 +030069static void omap_atomic_wait_for_completion(struct drm_device *dev,
70 struct drm_atomic_state *old_state)
71{
72 struct drm_crtc_state *old_crtc_state;
73 struct drm_crtc *crtc;
74 unsigned int i;
75 int ret;
76
77 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
78 if (!crtc->state->enable)
79 continue;
80
81 ret = omap_crtc_wait_pending(crtc);
82
83 if (!ret)
84 dev_warn(dev->dev,
85 "atomic complete timeout (pipe %u)!\n", i);
86 }
87}
88
Laurent Pinchart748471a52015-03-05 23:42:39 +020089static void omap_atomic_complete(struct omap_atomic_state_commit *commit)
90{
91 struct drm_device *dev = commit->dev;
92 struct omap_drm_private *priv = dev->dev_private;
93 struct drm_atomic_state *old_state = commit->state;
94
95 /* Apply the atomic update. */
Tomi Valkeinen9f759222015-11-05 18:39:52 +020096 priv->dispc_ops->runtime_get();
Laurent Pinchart69fb7c82015-05-28 02:09:56 +030097
Laurent Pinchart748471a52015-03-05 23:42:39 +020098 drm_atomic_helper_commit_modeset_disables(dev, old_state);
Jyri Sarha897145d2017-01-27 12:04:55 +020099
100 /* With the current dss dispc implementation we have to enable
101 * the new modeset before we can commit planes. The dispc ovl
102 * configuration relies on the video mode configuration been
103 * written into the HW when the ovl configuration is
104 * calculated.
105 *
106 * This approach is not ideal because after a mode change the
107 * plane update is executed only after the first vblank
108 * interrupt. The dispc implementation should be fixed so that
109 * it is able use uncommitted drm state information.
110 */
Laurent Pinchart748471a52015-03-05 23:42:39 +0200111 drm_atomic_helper_commit_modeset_enables(dev, old_state);
Jyri Sarha897145d2017-01-27 12:04:55 +0200112 omap_atomic_wait_for_completion(dev, old_state);
113
114 drm_atomic_helper_commit_planes(dev, old_state, 0);
Laurent Pinchart748471a52015-03-05 23:42:39 +0200115
Tomi Valkeinen5f741b32015-05-29 16:01:18 +0300116 omap_atomic_wait_for_completion(dev, old_state);
Laurent Pinchart748471a52015-03-05 23:42:39 +0200117
118 drm_atomic_helper_cleanup_planes(dev, old_state);
119
Tomi Valkeinen9f759222015-11-05 18:39:52 +0200120 priv->dispc_ops->runtime_put();
Laurent Pinchart69fb7c82015-05-28 02:09:56 +0300121
Chris Wilson08536952016-10-14 13:18:18 +0100122 drm_atomic_state_put(old_state);
Laurent Pinchart748471a52015-03-05 23:42:39 +0200123
124 /* Complete the commit, wake up any waiter. */
125 spin_lock(&priv->commit.lock);
126 priv->commit.pending &= ~commit->crtcs;
127 spin_unlock(&priv->commit.lock);
128
129 wake_up_all(&priv->commit.wait);
130
131 kfree(commit);
132}
133
134static void omap_atomic_work(struct work_struct *work)
135{
136 struct omap_atomic_state_commit *commit =
137 container_of(work, struct omap_atomic_state_commit, work);
138
139 omap_atomic_complete(commit);
140}
141
142static bool omap_atomic_is_pending(struct omap_drm_private *priv,
143 struct omap_atomic_state_commit *commit)
144{
145 bool pending;
146
147 spin_lock(&priv->commit.lock);
148 pending = priv->commit.pending & commit->crtcs;
149 spin_unlock(&priv->commit.lock);
150
151 return pending;
152}
153
154static int omap_atomic_commit(struct drm_device *dev,
Maarten Lankhorst6fc17fb2016-04-26 16:11:39 +0200155 struct drm_atomic_state *state, bool nonblock)
Laurent Pinchart748471a52015-03-05 23:42:39 +0200156{
157 struct omap_drm_private *priv = dev->dev_private;
158 struct omap_atomic_state_commit *commit;
Daniel Vetter82072572016-06-02 00:06:29 +0200159 struct drm_crtc *crtc;
160 struct drm_crtc_state *crtc_state;
161 int i, ret;
Laurent Pinchart748471a52015-03-05 23:42:39 +0200162
163 ret = drm_atomic_helper_prepare_planes(dev, state);
164 if (ret)
165 return ret;
166
167 /* Allocate the commit object. */
168 commit = kzalloc(sizeof(*commit), GFP_KERNEL);
169 if (commit == NULL) {
170 ret = -ENOMEM;
171 goto error;
172 }
173
174 INIT_WORK(&commit->work, omap_atomic_work);
175 commit->dev = dev;
176 commit->state = state;
177
178 /* Wait until all affected CRTCs have completed previous commits and
179 * mark them as pending.
180 */
Daniel Vetter82072572016-06-02 00:06:29 +0200181 for_each_crtc_in_state(state, crtc, crtc_state, i)
182 commit->crtcs |= drm_crtc_mask(crtc);
Laurent Pinchart748471a52015-03-05 23:42:39 +0200183
184 wait_event(priv->commit.wait, !omap_atomic_is_pending(priv, commit));
185
186 spin_lock(&priv->commit.lock);
187 priv->commit.pending |= commit->crtcs;
188 spin_unlock(&priv->commit.lock);
189
190 /* Swap the state, this is the point of no return. */
Daniel Vetter5e84c262016-06-10 00:06:32 +0200191 drm_atomic_helper_swap_state(state, true);
Laurent Pinchart748471a52015-03-05 23:42:39 +0200192
Chris Wilson08536952016-10-14 13:18:18 +0100193 drm_atomic_state_get(state);
Maarten Lankhorst6fc17fb2016-04-26 16:11:39 +0200194 if (nonblock)
Laurent Pinchart748471a52015-03-05 23:42:39 +0200195 schedule_work(&commit->work);
196 else
197 omap_atomic_complete(commit);
198
199 return 0;
200
201error:
202 drm_atomic_helper_cleanup_planes(dev, state);
203 return ret;
204}
205
Laurent Pincharte6ecefa2012-05-17 13:27:23 +0200206static const struct drm_mode_config_funcs omap_mode_config_funcs = {
Rob Clarkcd5351f2011-11-12 12:09:40 -0600207 .fb_create = omap_framebuffer_create,
208 .output_poll_changed = omap_fb_output_poll_changed,
Laurent Pinchartcef77d42015-03-05 21:50:00 +0200209 .atomic_check = drm_atomic_helper_check,
Laurent Pinchart748471a52015-03-05 23:42:39 +0200210 .atomic_commit = omap_atomic_commit,
Rob Clarkcd5351f2011-11-12 12:09:40 -0600211};
212
213static int get_connector_type(struct omap_dss_device *dssdev)
214{
215 switch (dssdev->type) {
216 case OMAP_DISPLAY_TYPE_HDMI:
217 return DRM_MODE_CONNECTOR_HDMIA;
Tomi Valkeinen4635c172013-05-14 14:14:15 +0300218 case OMAP_DISPLAY_TYPE_DVI:
219 return DRM_MODE_CONNECTOR_DVID;
Sebastian Reichel4a64b902016-03-08 17:39:36 +0100220 case OMAP_DISPLAY_TYPE_DSI:
221 return DRM_MODE_CONNECTOR_DSI;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600222 default:
223 return DRM_MODE_CONNECTOR_Unknown;
224 }
225}
226
Archit Taneja0d8f3712013-03-26 19:15:19 +0530227static bool channel_used(struct drm_device *dev, enum omap_channel channel)
228{
229 struct omap_drm_private *priv = dev->dev_private;
230 int i;
231
232 for (i = 0; i < priv->num_crtcs; i++) {
233 struct drm_crtc *crtc = priv->crtcs[i];
234
235 if (omap_crtc_channel(crtc) == channel)
236 return true;
237 }
238
239 return false;
240}
Archit Tanejacc823bd2014-01-02 14:49:52 +0530241static void omap_disconnect_dssdevs(void)
242{
243 struct omap_dss_device *dssdev = NULL;
244
245 for_each_dss_dev(dssdev)
246 dssdev->driver->disconnect(dssdev);
247}
Archit Taneja0d8f3712013-03-26 19:15:19 +0530248
Archit Taneja3a01ab22014-01-02 14:49:51 +0530249static int omap_connect_dssdevs(void)
250{
251 int r;
252 struct omap_dss_device *dssdev = NULL;
Peter Ujfalusia09d2bc2016-05-03 22:08:01 +0300253
254 if (!omapdss_stack_is_ready())
255 return -EPROBE_DEFER;
Archit Taneja3a01ab22014-01-02 14:49:51 +0530256
257 for_each_dss_dev(dssdev) {
258 r = dssdev->driver->connect(dssdev);
259 if (r == -EPROBE_DEFER) {
260 omap_dss_put_device(dssdev);
261 goto cleanup;
262 } else if (r) {
263 dev_warn(dssdev->dev, "could not connect display: %s\n",
264 dssdev->name);
Archit Taneja3a01ab22014-01-02 14:49:51 +0530265 }
266 }
267
Archit Taneja3a01ab22014-01-02 14:49:51 +0530268 return 0;
269
270cleanup:
271 /*
272 * if we are deferring probe, we disconnect the devices we previously
273 * connected
274 */
Archit Tanejacc823bd2014-01-02 14:49:52 +0530275 omap_disconnect_dssdevs();
Archit Taneja3a01ab22014-01-02 14:49:51 +0530276
277 return r;
278}
Rob Clarkcd5351f2011-11-12 12:09:40 -0600279
Laurent Pinchartfb9a35f2015-01-11 16:30:44 +0200280static int omap_modeset_create_crtc(struct drm_device *dev, int id,
Tomi Valkeinene43f2c32016-12-02 16:07:11 +0200281 enum omap_channel channel,
282 u32 possible_crtcs)
Laurent Pinchartfb9a35f2015-01-11 16:30:44 +0200283{
284 struct omap_drm_private *priv = dev->dev_private;
285 struct drm_plane *plane;
286 struct drm_crtc *crtc;
287
Tomi Valkeinene43f2c32016-12-02 16:07:11 +0200288 plane = omap_plane_init(dev, id, DRM_PLANE_TYPE_PRIMARY,
289 possible_crtcs);
Laurent Pinchartfb9a35f2015-01-11 16:30:44 +0200290 if (IS_ERR(plane))
291 return PTR_ERR(plane);
292
293 crtc = omap_crtc_init(dev, plane, channel, id);
294
295 BUG_ON(priv->num_crtcs >= ARRAY_SIZE(priv->crtcs));
296 priv->crtcs[id] = crtc;
297 priv->num_crtcs++;
298
299 priv->planes[id] = plane;
300 priv->num_planes++;
301
302 return 0;
303}
304
Laurent Pincharte2cd09b2015-03-06 17:16:43 +0200305static int omap_modeset_init_properties(struct drm_device *dev)
306{
307 struct omap_drm_private *priv = dev->dev_private;
308
Laurent Pincharte2cd09b2015-03-06 17:16:43 +0200309 priv->zorder_prop = drm_property_create_range(dev, 0, "zorder", 0, 3);
310 if (!priv->zorder_prop)
311 return -ENOMEM;
312
313 return 0;
314}
315
Rob Clarkcd5351f2011-11-12 12:09:40 -0600316static int omap_modeset_init(struct drm_device *dev)
317{
Rob Clarkcd5351f2011-11-12 12:09:40 -0600318 struct omap_drm_private *priv = dev->dev_private;
319 struct omap_dss_device *dssdev = NULL;
Tomi Valkeinen9f759222015-11-05 18:39:52 +0200320 int num_ovls = priv->dispc_ops->get_num_ovls();
321 int num_mgrs = priv->dispc_ops->get_num_mgrs();
Archit Taneja0d8f3712013-03-26 19:15:19 +0530322 int num_crtcs;
323 int i, id = 0;
Laurent Pinchartfb9a35f2015-01-11 16:30:44 +0200324 int ret;
Tomi Valkeinene43f2c32016-12-02 16:07:11 +0200325 u32 possible_crtcs;
Tomi Valkeinen04b1fc02013-05-14 10:55:19 +0300326
Rob Clarkcd5351f2011-11-12 12:09:40 -0600327 drm_mode_config_init(dev);
328
Laurent Pincharte2cd09b2015-03-06 17:16:43 +0200329 ret = omap_modeset_init_properties(dev);
330 if (ret < 0)
331 return ret;
332
Rob Clarkf5f94542012-12-04 13:59:12 -0600333 /*
Archit Taneja0d8f3712013-03-26 19:15:19 +0530334 * We usually don't want to create a CRTC for each manager, at least
335 * not until we have a way to expose private planes to userspace.
336 * Otherwise there would not be enough video pipes left for drm planes.
337 * We use the num_crtc argument to limit the number of crtcs we create.
Rob Clarkf5f94542012-12-04 13:59:12 -0600338 */
Archit Taneja0d8f3712013-03-26 19:15:19 +0530339 num_crtcs = min3(num_crtc, num_mgrs, num_ovls);
Tomi Valkeinene43f2c32016-12-02 16:07:11 +0200340 possible_crtcs = (1 << num_crtcs) - 1;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600341
Archit Taneja0d8f3712013-03-26 19:15:19 +0530342 dssdev = NULL;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600343
Rob Clarkf5f94542012-12-04 13:59:12 -0600344 for_each_dss_dev(dssdev) {
345 struct drm_connector *connector;
346 struct drm_encoder *encoder;
Archit Taneja0d8f3712013-03-26 19:15:19 +0530347 enum omap_channel channel;
Tomi Valkeinen179df152015-10-21 16:17:23 +0300348 struct omap_dss_device *out;
Rob Clarkf5f94542012-12-04 13:59:12 -0600349
Archit Taneja3a01ab22014-01-02 14:49:51 +0530350 if (!omapdss_device_is_connected(dssdev))
Archit Taneja581382e2013-03-26 19:15:18 +0530351 continue;
Tomi Valkeinena7e71e72013-05-08 16:23:32 +0300352
Rob Clarkf5f94542012-12-04 13:59:12 -0600353 encoder = omap_encoder_init(dev, dssdev);
354
355 if (!encoder) {
356 dev_err(dev->dev, "could not create encoder: %s\n",
357 dssdev->name);
358 return -ENOMEM;
359 }
360
361 connector = omap_connector_init(dev,
362 get_connector_type(dssdev), dssdev, encoder);
363
364 if (!connector) {
365 dev_err(dev->dev, "could not create connector: %s\n",
366 dssdev->name);
367 return -ENOMEM;
368 }
369
370 BUG_ON(priv->num_encoders >= ARRAY_SIZE(priv->encoders));
371 BUG_ON(priv->num_connectors >= ARRAY_SIZE(priv->connectors));
372
373 priv->encoders[priv->num_encoders++] = encoder;
374 priv->connectors[priv->num_connectors++] = connector;
375
376 drm_mode_connector_attach_encoder(connector, encoder);
377
Archit Taneja0d8f3712013-03-26 19:15:19 +0530378 /*
379 * if we have reached the limit of the crtcs we are allowed to
380 * create, let's not try to look for a crtc for this
381 * panel/encoder and onwards, we will, of course, populate the
382 * the possible_crtcs field for all the encoders with the final
383 * set of crtcs we create
384 */
385 if (id == num_crtcs)
386 continue;
387
388 /*
389 * get the recommended DISPC channel for this encoder. For now,
390 * we only try to get create a crtc out of the recommended, the
391 * other possible channels to which the encoder can connect are
392 * not considered.
393 */
Archit Taneja0d8f3712013-03-26 19:15:19 +0530394
Tomi Valkeinen179df152015-10-21 16:17:23 +0300395 out = omapdss_find_output_from_display(dssdev);
396 channel = out->dispc_channel;
397 omap_dss_put_device(out);
398
Archit Taneja0d8f3712013-03-26 19:15:19 +0530399 /*
400 * if this channel hasn't already been taken by a previously
401 * allocated crtc, we create a new crtc for it
402 */
403 if (!channel_used(dev, channel)) {
Tomi Valkeinene43f2c32016-12-02 16:07:11 +0200404 ret = omap_modeset_create_crtc(dev, id, channel,
405 possible_crtcs);
Laurent Pinchartfb9a35f2015-01-11 16:30:44 +0200406 if (ret < 0) {
407 dev_err(dev->dev,
408 "could not create CRTC (channel %u)\n",
409 channel);
410 return ret;
411 }
Archit Taneja0d8f3712013-03-26 19:15:19 +0530412
413 id++;
414 }
415 }
416
417 /*
418 * we have allocated crtcs according to the need of the panels/encoders,
419 * adding more crtcs here if needed
420 */
421 for (; id < num_crtcs; id++) {
422
423 /* find a free manager for this crtc */
424 for (i = 0; i < num_mgrs; i++) {
Laurent Pinchartfb9a35f2015-01-11 16:30:44 +0200425 if (!channel_used(dev, i))
Archit Taneja0d8f3712013-03-26 19:15:19 +0530426 break;
Archit Taneja0d8f3712013-03-26 19:15:19 +0530427 }
428
429 if (i == num_mgrs) {
430 /* this shouldn't really happen */
431 dev_err(dev->dev, "no managers left for crtc\n");
432 return -ENOMEM;
433 }
Laurent Pinchartfb9a35f2015-01-11 16:30:44 +0200434
Tomi Valkeinene43f2c32016-12-02 16:07:11 +0200435 ret = omap_modeset_create_crtc(dev, id, i,
436 possible_crtcs);
Laurent Pinchartfb9a35f2015-01-11 16:30:44 +0200437 if (ret < 0) {
438 dev_err(dev->dev,
439 "could not create CRTC (channel %u)\n", i);
440 return ret;
441 }
Archit Taneja0d8f3712013-03-26 19:15:19 +0530442 }
443
444 /*
445 * Create normal planes for the remaining overlays:
446 */
447 for (; id < num_ovls; id++) {
Laurent Pinchartfb9a35f2015-01-11 16:30:44 +0200448 struct drm_plane *plane;
449
Tomi Valkeinene43f2c32016-12-02 16:07:11 +0200450 plane = omap_plane_init(dev, id, DRM_PLANE_TYPE_OVERLAY,
451 possible_crtcs);
Laurent Pinchartfb9a35f2015-01-11 16:30:44 +0200452 if (IS_ERR(plane))
453 return PTR_ERR(plane);
Archit Taneja0d8f3712013-03-26 19:15:19 +0530454
455 BUG_ON(priv->num_planes >= ARRAY_SIZE(priv->planes));
456 priv->planes[priv->num_planes++] = plane;
457 }
458
459 for (i = 0; i < priv->num_encoders; i++) {
460 struct drm_encoder *encoder = priv->encoders[i];
461 struct omap_dss_device *dssdev =
462 omap_encoder_get_dssdev(encoder);
Tomi Valkeinen1f68d9c2013-04-19 15:09:34 +0300463 struct omap_dss_device *output;
Tomi Valkeinenbe8e8e12013-04-23 15:35:35 +0300464
465 output = omapdss_find_output_from_display(dssdev);
Archit Taneja0d8f3712013-03-26 19:15:19 +0530466
Rob Clarkf5f94542012-12-04 13:59:12 -0600467 /* figure out which crtc's we can connect the encoder to: */
468 encoder->possible_crtcs = 0;
469 for (id = 0; id < priv->num_crtcs; id++) {
Archit Taneja0d8f3712013-03-26 19:15:19 +0530470 struct drm_crtc *crtc = priv->crtcs[id];
471 enum omap_channel crtc_channel;
Archit Taneja0d8f3712013-03-26 19:15:19 +0530472
473 crtc_channel = omap_crtc_channel(crtc);
Archit Taneja0d8f3712013-03-26 19:15:19 +0530474
Tomi Valkeinen17337292014-09-03 19:25:49 +0000475 if (output->dispc_channel == crtc_channel) {
Rob Clarkf5f94542012-12-04 13:59:12 -0600476 encoder->possible_crtcs |= (1 << id);
Tomi Valkeinen17337292014-09-03 19:25:49 +0000477 break;
478 }
Rob Clarkf5f94542012-12-04 13:59:12 -0600479 }
Tomi Valkeinen820caab2013-04-25 14:53:18 +0300480
481 omap_dss_put_device(output);
Rob Clarkf5f94542012-12-04 13:59:12 -0600482 }
Rob Clarkcd5351f2011-11-12 12:09:40 -0600483
Archit Taneja0d8f3712013-03-26 19:15:19 +0530484 DBG("registered %d planes, %d crtcs, %d encoders and %d connectors\n",
485 priv->num_planes, priv->num_crtcs, priv->num_encoders,
486 priv->num_connectors);
487
Tomi Valkeinen1e907112016-08-23 12:35:39 +0300488 dev->mode_config.min_width = 8;
489 dev->mode_config.min_height = 2;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600490
491 /* note: eventually will need some cpu_is_omapXYZ() type stuff here
492 * to fill in these limits properly on different OMAP generations..
493 */
494 dev->mode_config.max_width = 2048;
495 dev->mode_config.max_height = 2048;
496
497 dev->mode_config.funcs = &omap_mode_config_funcs;
498
Laurent Pinchart69a12262015-03-05 21:38:16 +0200499 drm_mode_config_reset(dev);
500
Laurent Pinchart728ae8d2015-05-28 00:21:29 +0300501 omap_drm_irq_install(dev);
502
Rob Clarkcd5351f2011-11-12 12:09:40 -0600503 return 0;
504}
505
Rob Clarkcd5351f2011-11-12 12:09:40 -0600506/*
507 * drm ioctl funcs
508 */
509
510
511static int ioctl_get_param(struct drm_device *dev, void *data,
512 struct drm_file *file_priv)
513{
Rob Clark5e3b0872012-10-29 09:31:12 +0100514 struct omap_drm_private *priv = dev->dev_private;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600515 struct drm_omap_param *args = data;
516
517 DBG("%p: param=%llu", dev, args->param);
518
519 switch (args->param) {
520 case OMAP_PARAM_CHIPSET_ID:
Rob Clark5e3b0872012-10-29 09:31:12 +0100521 args->value = priv->omaprev;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600522 break;
523 default:
524 DBG("unknown parameter %lld", args->param);
525 return -EINVAL;
526 }
527
528 return 0;
529}
530
531static int ioctl_set_param(struct drm_device *dev, void *data,
532 struct drm_file *file_priv)
533{
534 struct drm_omap_param *args = data;
535
536 switch (args->param) {
537 default:
538 DBG("unknown parameter %lld", args->param);
539 return -EINVAL;
540 }
541
542 return 0;
543}
544
Laurent Pinchartef3f4e92015-12-14 22:39:36 +0200545#define OMAP_BO_USER_MASK 0x00ffffff /* flags settable by userspace */
546
Rob Clarkcd5351f2011-11-12 12:09:40 -0600547static int ioctl_gem_new(struct drm_device *dev, void *data,
548 struct drm_file *file_priv)
549{
550 struct drm_omap_gem_new *args = data;
Laurent Pinchartef3f4e92015-12-14 22:39:36 +0200551 u32 flags = args->flags & OMAP_BO_USER_MASK;
552
Rob Clarkf5f94542012-12-04 13:59:12 -0600553 VERB("%p:%p: size=0x%08x, flags=%08x", dev, file_priv,
Laurent Pinchartef3f4e92015-12-14 22:39:36 +0200554 args->size.bytes, flags);
555
556 return omap_gem_new_handle(dev, file_priv, args->size, flags,
557 &args->handle);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600558}
559
560static int ioctl_gem_cpu_prep(struct drm_device *dev, void *data,
561 struct drm_file *file_priv)
562{
563 struct drm_omap_gem_cpu_prep *args = data;
564 struct drm_gem_object *obj;
565 int ret;
566
567 VERB("%p:%p: handle=%d, op=%x", dev, file_priv, args->handle, args->op);
568
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100569 obj = drm_gem_object_lookup(file_priv, args->handle);
YAMANE Toshiakic7f904b2012-11-14 19:30:38 +0900570 if (!obj)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600571 return -ENOENT;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600572
573 ret = omap_gem_op_sync(obj, args->op);
574
YAMANE Toshiakic7f904b2012-11-14 19:30:38 +0900575 if (!ret)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600576 ret = omap_gem_op_start(obj, args->op);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600577
578 drm_gem_object_unreference_unlocked(obj);
579
580 return ret;
581}
582
583static int ioctl_gem_cpu_fini(struct drm_device *dev, void *data,
584 struct drm_file *file_priv)
585{
586 struct drm_omap_gem_cpu_fini *args = data;
587 struct drm_gem_object *obj;
588 int ret;
589
590 VERB("%p:%p: handle=%d", dev, file_priv, args->handle);
591
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100592 obj = drm_gem_object_lookup(file_priv, args->handle);
YAMANE Toshiakic7f904b2012-11-14 19:30:38 +0900593 if (!obj)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600594 return -ENOENT;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600595
596 /* XXX flushy, flushy */
597 ret = 0;
598
YAMANE Toshiakic7f904b2012-11-14 19:30:38 +0900599 if (!ret)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600600 ret = omap_gem_op_finish(obj, args->op);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600601
602 drm_gem_object_unreference_unlocked(obj);
603
604 return ret;
605}
606
607static int ioctl_gem_info(struct drm_device *dev, void *data,
608 struct drm_file *file_priv)
609{
610 struct drm_omap_gem_info *args = data;
611 struct drm_gem_object *obj;
612 int ret = 0;
613
Rob Clarkf5f94542012-12-04 13:59:12 -0600614 VERB("%p:%p: handle=%d", dev, file_priv, args->handle);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600615
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100616 obj = drm_gem_object_lookup(file_priv, args->handle);
YAMANE Toshiakic7f904b2012-11-14 19:30:38 +0900617 if (!obj)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600618 return -ENOENT;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600619
Rob Clarkf7f9f452011-12-05 19:19:22 -0600620 args->size = omap_gem_mmap_size(obj);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600621 args->offset = omap_gem_mmap_offset(obj);
622
623 drm_gem_object_unreference_unlocked(obj);
624
625 return ret;
626}
627
Rob Clarkbaa70942013-08-02 13:27:49 -0400628static const struct drm_ioctl_desc ioctls[DRM_COMMAND_END - DRM_COMMAND_BASE] = {
Hemant Hariyani5f6ab8c2016-06-07 13:23:19 -0500629 DRM_IOCTL_DEF_DRV(OMAP_GET_PARAM, ioctl_get_param,
630 DRM_AUTH | DRM_RENDER_ALLOW),
631 DRM_IOCTL_DEF_DRV(OMAP_SET_PARAM, ioctl_set_param,
632 DRM_AUTH | DRM_MASTER | DRM_ROOT_ONLY),
633 DRM_IOCTL_DEF_DRV(OMAP_GEM_NEW, ioctl_gem_new,
634 DRM_AUTH | DRM_RENDER_ALLOW),
635 DRM_IOCTL_DEF_DRV(OMAP_GEM_CPU_PREP, ioctl_gem_cpu_prep,
636 DRM_AUTH | DRM_RENDER_ALLOW),
637 DRM_IOCTL_DEF_DRV(OMAP_GEM_CPU_FINI, ioctl_gem_cpu_fini,
638 DRM_AUTH | DRM_RENDER_ALLOW),
639 DRM_IOCTL_DEF_DRV(OMAP_GEM_INFO, ioctl_gem_info,
640 DRM_AUTH | DRM_RENDER_ALLOW),
Rob Clarkcd5351f2011-11-12 12:09:40 -0600641};
642
643/*
644 * drm driver funcs
645 */
646
Rob Clarkcd5351f2011-11-12 12:09:40 -0600647static int dev_open(struct drm_device *dev, struct drm_file *file)
648{
649 file->driver_priv = NULL;
650
651 DBG("open: dev=%p, file=%p", dev, file);
652
653 return 0;
654}
655
Rob Clarkcd5351f2011-11-12 12:09:40 -0600656/**
657 * lastclose - clean up after all DRM clients have exited
658 * @dev: DRM device
659 *
660 * Take care of cleaning up after all DRM clients have exited. In the
661 * mode setting case, we want to restore the kernel's initial mode (just
662 * in case the last client left us in a bad state).
663 */
664static void dev_lastclose(struct drm_device *dev)
665{
Rob Clark3c810c62012-08-15 15:18:01 -0500666 int i;
667
Lukas Wunnerf15a66e2015-09-05 11:22:39 +0200668 /* we don't support vga_switcheroo.. so just make sure the fbdev
Rob Clarkcd5351f2011-11-12 12:09:40 -0600669 * mode is active
670 */
671 struct omap_drm_private *priv = dev->dev_private;
672 int ret;
673
674 DBG("lastclose: dev=%p", dev);
675
Ville Syrjälä0da88db2016-09-26 19:30:52 +0300676 /* need to restore default rotation state.. not sure
677 * if there is a cleaner way to restore properties to
678 * default state? Maybe a flag that properties should
679 * automatically be restored to default state on
680 * lastclose?
681 */
682 for (i = 0; i < priv->num_crtcs; i++) {
683 struct drm_crtc *crtc = priv->crtcs[i];
Rob Clark3c810c62012-08-15 15:18:01 -0500684
Ville Syrjälä0da88db2016-09-26 19:30:52 +0300685 if (!crtc->primary->rotation_property)
686 continue;
687
688 drm_object_property_set_value(&crtc->base,
689 crtc->primary->rotation_property,
690 DRM_ROTATE_0);
691 }
692
693 for (i = 0; i < priv->num_planes; i++) {
694 struct drm_plane *plane = priv->planes[i];
695
696 if (!plane->rotation_property)
697 continue;
698
699 drm_object_property_set_value(&plane->base,
700 plane->rotation_property,
701 DRM_ROTATE_0);
Rob Clark3c810c62012-08-15 15:18:01 -0500702 }
703
Tomi Valkeinenc7c1aec2014-09-25 19:24:26 +0000704 if (priv->fbdev) {
705 ret = drm_fb_helper_restore_fbdev_mode_unlocked(priv->fbdev);
706 if (ret)
707 DBG("failed to restore crtc mode");
708 }
Rob Clarkcd5351f2011-11-12 12:09:40 -0600709}
710
Laurent Pinchart78b68552012-05-17 13:27:22 +0200711static const struct vm_operations_struct omap_gem_vm_ops = {
Rob Clarkcd5351f2011-11-12 12:09:40 -0600712 .fault = omap_gem_fault,
713 .open = drm_gem_vm_open,
714 .close = drm_gem_vm_close,
715};
716
Rob Clarkff4f3872012-01-16 12:51:14 -0600717static const struct file_operations omapdriver_fops = {
Laurent Pinchart222025e2015-01-11 00:02:07 +0200718 .owner = THIS_MODULE,
719 .open = drm_open,
720 .unlocked_ioctl = drm_ioctl,
721 .release = drm_release,
722 .mmap = omap_gem_mmap,
723 .poll = drm_poll,
724 .read = drm_read,
725 .llseek = noop_llseek,
Rob Clarkff4f3872012-01-16 12:51:14 -0600726};
727
Rob Clarkcd5351f2011-11-12 12:09:40 -0600728static struct drm_driver omap_drm_driver = {
Tomi Valkeinen728fea72015-10-02 11:10:41 +0300729 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME |
Hemant Hariyani5f6ab8c2016-06-07 13:23:19 -0500730 DRIVER_ATOMIC | DRIVER_RENDER,
Laurent Pinchart222025e2015-01-11 00:02:07 +0200731 .open = dev_open,
732 .lastclose = dev_lastclose,
Andy Gross6169a1482011-12-15 21:05:17 -0600733#ifdef CONFIG_DEBUG_FS
Laurent Pinchart222025e2015-01-11 00:02:07 +0200734 .debugfs_init = omap_debugfs_init,
Andy Gross6169a1482011-12-15 21:05:17 -0600735#endif
Laurent Pinchart222025e2015-01-11 00:02:07 +0200736 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
737 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
738 .gem_prime_export = omap_gem_prime_export,
739 .gem_prime_import = omap_gem_prime_import,
740 .gem_free_object = omap_gem_free_object,
741 .gem_vm_ops = &omap_gem_vm_ops,
742 .dumb_create = omap_gem_dumb_create,
743 .dumb_map_offset = omap_gem_dumb_map_offset,
744 .dumb_destroy = drm_gem_dumb_destroy,
745 .ioctls = ioctls,
746 .num_ioctls = DRM_OMAP_NUM_IOCTLS,
747 .fops = &omapdriver_fops,
748 .name = DRIVER_NAME,
749 .desc = DRIVER_DESC,
750 .date = DRIVER_DATE,
751 .major = DRIVER_MAJOR,
752 .minor = DRIVER_MINOR,
753 .patchlevel = DRIVER_PATCHLEVEL,
Rob Clarkcd5351f2011-11-12 12:09:40 -0600754};
755
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200756static int pdev_probe(struct platform_device *pdev)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600757{
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200758 struct omap_drm_platform_data *pdata = pdev->dev.platform_data;
759 struct omap_drm_private *priv;
760 struct drm_device *ddev;
761 unsigned int i;
762 int ret;
763
764 DBG("%s", pdev->name);
Archit Taneja3a01ab22014-01-02 14:49:51 +0530765
Tomi Valkeinen591a0ac2013-05-23 12:07:50 +0300766 if (omapdss_is_initialized() == false)
767 return -EPROBE_DEFER;
768
Archit Taneja3a01ab22014-01-02 14:49:51 +0530769 omap_crtc_pre_init();
770
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200771 ret = omap_connect_dssdevs();
772 if (ret)
773 goto err_crtc_uninit;
774
775 /* Allocate and initialize the driver private structure. */
776 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
777 if (!priv) {
778 ret = -ENOMEM;
779 goto err_disconnect_dssdevs;
Archit Taneja3a01ab22014-01-02 14:49:51 +0530780 }
781
Tomi Valkeinen9f759222015-11-05 18:39:52 +0200782 priv->dispc_ops = dispc_get_ops();
783
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200784 priv->omaprev = pdata->omaprev;
785 priv->wq = alloc_ordered_workqueue("omapdrm", 0);
786
787 init_waitqueue_head(&priv->commit.wait);
788 spin_lock_init(&priv->commit.lock);
789 spin_lock_init(&priv->list_lock);
790 INIT_LIST_HEAD(&priv->obj_list);
791
792 /* Allocate and initialize the DRM device. */
793 ddev = drm_dev_alloc(&omap_drm_driver, &pdev->dev);
794 if (IS_ERR(ddev)) {
795 ret = PTR_ERR(ddev);
796 goto err_free_priv;
797 }
798
799 ddev->dev_private = priv;
800 platform_set_drvdata(pdev, ddev);
801
802 omap_gem_init(ddev);
803
804 ret = omap_modeset_init(ddev);
805 if (ret) {
806 dev_err(&pdev->dev, "omap_modeset_init failed: ret=%d\n", ret);
807 goto err_free_drm_dev;
808 }
809
810 /* Initialize vblank handling, start with all CRTCs disabled. */
811 ret = drm_vblank_init(ddev, priv->num_crtcs);
812 if (ret) {
813 dev_err(&pdev->dev, "could not init vblank\n");
814 goto err_cleanup_modeset;
815 }
816
817 for (i = 0; i < priv->num_crtcs; i++)
818 drm_crtc_vblank_off(priv->crtcs[i]);
819
820 priv->fbdev = omap_fbdev_init(ddev);
821
822 drm_kms_helper_poll_init(ddev);
823
824 /*
825 * Register the DRM device with the core and the connectors with
826 * sysfs.
827 */
828 ret = drm_dev_register(ddev, 0);
829 if (ret)
830 goto err_cleanup_helpers;
831
832 return 0;
833
834err_cleanup_helpers:
835 drm_kms_helper_poll_fini(ddev);
836 if (priv->fbdev)
837 omap_fbdev_free(ddev);
838err_cleanup_modeset:
839 drm_mode_config_cleanup(ddev);
840 omap_drm_irq_uninstall(ddev);
841err_free_drm_dev:
842 omap_gem_deinit(ddev);
843 drm_dev_unref(ddev);
844err_free_priv:
845 destroy_workqueue(priv->wq);
846 kfree(priv);
847err_disconnect_dssdevs:
848 omap_disconnect_dssdevs();
849err_crtc_uninit:
850 omap_crtc_pre_uninit();
851 return ret;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600852}
853
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200854static int pdev_remove(struct platform_device *pdev)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600855{
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200856 struct drm_device *ddev = platform_get_drvdata(pdev);
857 struct omap_drm_private *priv = ddev->dev_private;
858
Rob Clarkcd5351f2011-11-12 12:09:40 -0600859 DBG("");
Andy Gross5c137792012-03-05 10:48:39 -0600860
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200861 drm_dev_unregister(ddev);
862
863 drm_kms_helper_poll_fini(ddev);
864
865 if (priv->fbdev)
866 omap_fbdev_free(ddev);
867
Tomi Valkeinen8a54aa92017-03-27 10:02:22 +0300868 drm_atomic_helper_shutdown(ddev);
869
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200870 drm_mode_config_cleanup(ddev);
871
872 omap_drm_irq_uninstall(ddev);
873 omap_gem_deinit(ddev);
874
875 drm_dev_unref(ddev);
876
877 destroy_workqueue(priv->wq);
878 kfree(priv);
Tomi Valkeinen707cf582014-04-02 13:47:43 +0300879
Archit Tanejacc823bd2014-01-02 14:49:52 +0530880 omap_disconnect_dssdevs();
881 omap_crtc_pre_uninit();
Daniel Vetterfd3c0252013-12-11 11:34:26 +0100882
Rob Clarkcd5351f2011-11-12 12:09:40 -0600883 return 0;
884}
885
Grygorii Strashko8450c8d2015-02-26 15:57:17 +0200886#ifdef CONFIG_PM_SLEEP
Tomi Valkeinen92bf0f92015-10-02 11:10:42 +0300887static int omap_drm_suspend_all_displays(void)
888{
889 struct omap_dss_device *dssdev = NULL;
890
891 for_each_dss_dev(dssdev) {
892 if (!dssdev->driver)
893 continue;
894
895 if (dssdev->state == OMAP_DSS_DISPLAY_ACTIVE) {
896 dssdev->driver->disable(dssdev);
897 dssdev->activate_after_resume = true;
898 } else {
899 dssdev->activate_after_resume = false;
900 }
901 }
902
903 return 0;
904}
905
906static int omap_drm_resume_all_displays(void)
907{
908 struct omap_dss_device *dssdev = NULL;
909
910 for_each_dss_dev(dssdev) {
911 if (!dssdev->driver)
912 continue;
913
914 if (dssdev->activate_after_resume) {
915 dssdev->driver->enable(dssdev);
916 dssdev->activate_after_resume = false;
917 }
918 }
919
920 return 0;
921}
922
Tomi Valkeinenccd7b5e2014-11-14 15:18:28 +0200923static int omap_drm_suspend(struct device *dev)
924{
925 struct drm_device *drm_dev = dev_get_drvdata(dev);
926
927 drm_kms_helper_poll_disable(drm_dev);
928
Tomi Valkeinen92bf0f92015-10-02 11:10:42 +0300929 drm_modeset_lock_all(drm_dev);
930 omap_drm_suspend_all_displays();
931 drm_modeset_unlock_all(drm_dev);
932
Tomi Valkeinenccd7b5e2014-11-14 15:18:28 +0200933 return 0;
934}
935
936static int omap_drm_resume(struct device *dev)
937{
938 struct drm_device *drm_dev = dev_get_drvdata(dev);
939
Tomi Valkeinen92bf0f92015-10-02 11:10:42 +0300940 drm_modeset_lock_all(drm_dev);
941 omap_drm_resume_all_displays();
942 drm_modeset_unlock_all(drm_dev);
943
Tomi Valkeinenccd7b5e2014-11-14 15:18:28 +0200944 drm_kms_helper_poll_enable(drm_dev);
945
946 return omap_gem_resume(dev);
947}
Andy Grosse78edba2012-12-19 14:53:37 -0600948#endif
949
Grygorii Strashko8450c8d2015-02-26 15:57:17 +0200950static SIMPLE_DEV_PM_OPS(omapdrm_pm_ops, omap_drm_suspend, omap_drm_resume);
951
Tomi Valkeinen6717cd22013-04-10 10:44:00 +0300952static struct platform_driver pdev = {
Laurent Pinchart222025e2015-01-11 00:02:07 +0200953 .driver = {
954 .name = DRIVER_NAME,
Laurent Pinchart222025e2015-01-11 00:02:07 +0200955 .pm = &omapdrm_pm_ops,
Laurent Pinchart222025e2015-01-11 00:02:07 +0200956 },
957 .probe = pdev_probe,
958 .remove = pdev_remove,
Rob Clarkcd5351f2011-11-12 12:09:40 -0600959};
960
Thierry Redinge1c49bd2015-12-02 17:23:31 +0100961static struct platform_driver * const drivers[] = {
962 &omap_dmm_driver,
963 &pdev,
964};
965
Rob Clarkcd5351f2011-11-12 12:09:40 -0600966static int __init omap_drm_init(void)
967{
968 DBG("init");
Tomi Valkeinenea7e3a62014-04-02 14:31:50 +0300969
Thierry Redinge1c49bd2015-12-02 17:23:31 +0100970 return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
Rob Clarkcd5351f2011-11-12 12:09:40 -0600971}
972
973static void __exit omap_drm_fini(void)
974{
975 DBG("fini");
Tomi Valkeinenea7e3a62014-04-02 14:31:50 +0300976
Thierry Redinge1c49bd2015-12-02 17:23:31 +0100977 platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
Rob Clarkcd5351f2011-11-12 12:09:40 -0600978}
979
980/* need late_initcall() so we load after dss_driver's are loaded */
981late_initcall(omap_drm_init);
982module_exit(omap_drm_fini);
983
984MODULE_AUTHOR("Rob Clark <rob@ti.com>");
985MODULE_DESCRIPTION("OMAP DRM Display Driver");
986MODULE_ALIAS("platform:" DRIVER_NAME);
987MODULE_LICENSE("GPL v2");