blob: a8927cfebdac405608b4a85abcdb3cbb4b60ed7a [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 -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
Laurent Pinchart748471a52015-03-05 23:42:39 +020057struct omap_atomic_state_commit {
58 struct work_struct work;
59 struct drm_device *dev;
60 struct drm_atomic_state *state;
61 u32 crtcs;
62};
63
Tomi Valkeinen5f741b32015-05-29 16:01:18 +030064static void omap_atomic_wait_for_completion(struct drm_device *dev,
65 struct drm_atomic_state *old_state)
66{
67 struct drm_crtc_state *old_crtc_state;
68 struct drm_crtc *crtc;
69 unsigned int i;
70 int ret;
71
72 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
73 if (!crtc->state->enable)
74 continue;
75
76 ret = omap_crtc_wait_pending(crtc);
77
78 if (!ret)
79 dev_warn(dev->dev,
80 "atomic complete timeout (pipe %u)!\n", i);
81 }
82}
83
Laurent Pinchart748471a52015-03-05 23:42:39 +020084static void omap_atomic_complete(struct omap_atomic_state_commit *commit)
85{
86 struct drm_device *dev = commit->dev;
87 struct omap_drm_private *priv = dev->dev_private;
88 struct drm_atomic_state *old_state = commit->state;
89
90 /* Apply the atomic update. */
Tomi Valkeinen9f759222015-11-05 18:39:52 +020091 priv->dispc_ops->runtime_get();
Laurent Pinchart69fb7c82015-05-28 02:09:56 +030092
Laurent Pinchart748471a52015-03-05 23:42:39 +020093 drm_atomic_helper_commit_modeset_disables(dev, old_state);
Jyri Sarha897145d2017-01-27 12:04:55 +020094
95 /* With the current dss dispc implementation we have to enable
96 * the new modeset before we can commit planes. The dispc ovl
97 * configuration relies on the video mode configuration been
98 * written into the HW when the ovl configuration is
99 * calculated.
100 *
101 * This approach is not ideal because after a mode change the
102 * plane update is executed only after the first vblank
103 * interrupt. The dispc implementation should be fixed so that
104 * it is able use uncommitted drm state information.
105 */
Laurent Pinchart748471a52015-03-05 23:42:39 +0200106 drm_atomic_helper_commit_modeset_enables(dev, old_state);
Jyri Sarha897145d2017-01-27 12:04:55 +0200107 omap_atomic_wait_for_completion(dev, old_state);
108
109 drm_atomic_helper_commit_planes(dev, old_state, 0);
Laurent Pinchart748471a52015-03-05 23:42:39 +0200110
Tomi Valkeinen5f741b32015-05-29 16:01:18 +0300111 omap_atomic_wait_for_completion(dev, old_state);
Laurent Pinchart748471a52015-03-05 23:42:39 +0200112
113 drm_atomic_helper_cleanup_planes(dev, old_state);
114
Tomi Valkeinen9f759222015-11-05 18:39:52 +0200115 priv->dispc_ops->runtime_put();
Laurent Pinchart69fb7c82015-05-28 02:09:56 +0300116
Chris Wilson08536952016-10-14 13:18:18 +0100117 drm_atomic_state_put(old_state);
Laurent Pinchart748471a52015-03-05 23:42:39 +0200118
119 /* Complete the commit, wake up any waiter. */
120 spin_lock(&priv->commit.lock);
121 priv->commit.pending &= ~commit->crtcs;
122 spin_unlock(&priv->commit.lock);
123
124 wake_up_all(&priv->commit.wait);
125
126 kfree(commit);
127}
128
129static void omap_atomic_work(struct work_struct *work)
130{
131 struct omap_atomic_state_commit *commit =
132 container_of(work, struct omap_atomic_state_commit, work);
133
134 omap_atomic_complete(commit);
135}
136
137static bool omap_atomic_is_pending(struct omap_drm_private *priv,
138 struct omap_atomic_state_commit *commit)
139{
140 bool pending;
141
142 spin_lock(&priv->commit.lock);
143 pending = priv->commit.pending & commit->crtcs;
144 spin_unlock(&priv->commit.lock);
145
146 return pending;
147}
148
149static int omap_atomic_commit(struct drm_device *dev,
Maarten Lankhorst6fc17fb2016-04-26 16:11:39 +0200150 struct drm_atomic_state *state, bool nonblock)
Laurent Pinchart748471a52015-03-05 23:42:39 +0200151{
152 struct omap_drm_private *priv = dev->dev_private;
153 struct omap_atomic_state_commit *commit;
Daniel Vetter82072572016-06-02 00:06:29 +0200154 struct drm_crtc *crtc;
155 struct drm_crtc_state *crtc_state;
156 int i, ret;
Laurent Pinchart748471a52015-03-05 23:42:39 +0200157
158 ret = drm_atomic_helper_prepare_planes(dev, state);
159 if (ret)
160 return ret;
161
162 /* Allocate the commit object. */
163 commit = kzalloc(sizeof(*commit), GFP_KERNEL);
164 if (commit == NULL) {
165 ret = -ENOMEM;
166 goto error;
167 }
168
169 INIT_WORK(&commit->work, omap_atomic_work);
170 commit->dev = dev;
171 commit->state = state;
172
173 /* Wait until all affected CRTCs have completed previous commits and
174 * mark them as pending.
175 */
Daniel Vetter82072572016-06-02 00:06:29 +0200176 for_each_crtc_in_state(state, crtc, crtc_state, i)
177 commit->crtcs |= drm_crtc_mask(crtc);
Laurent Pinchart748471a52015-03-05 23:42:39 +0200178
179 wait_event(priv->commit.wait, !omap_atomic_is_pending(priv, commit));
180
181 spin_lock(&priv->commit.lock);
182 priv->commit.pending |= commit->crtcs;
183 spin_unlock(&priv->commit.lock);
184
185 /* Swap the state, this is the point of no return. */
Daniel Vetter5e84c262016-06-10 00:06:32 +0200186 drm_atomic_helper_swap_state(state, true);
Laurent Pinchart748471a52015-03-05 23:42:39 +0200187
Chris Wilson08536952016-10-14 13:18:18 +0100188 drm_atomic_state_get(state);
Maarten Lankhorst6fc17fb2016-04-26 16:11:39 +0200189 if (nonblock)
Laurent Pinchart748471a52015-03-05 23:42:39 +0200190 schedule_work(&commit->work);
191 else
192 omap_atomic_complete(commit);
193
194 return 0;
195
196error:
197 drm_atomic_helper_cleanup_planes(dev, state);
198 return ret;
199}
200
Laurent Pincharte6ecefa2012-05-17 13:27:23 +0200201static const struct drm_mode_config_funcs omap_mode_config_funcs = {
Rob Clarkcd5351f2011-11-12 12:09:40 -0600202 .fb_create = omap_framebuffer_create,
203 .output_poll_changed = omap_fb_output_poll_changed,
Laurent Pinchartcef77d42015-03-05 21:50:00 +0200204 .atomic_check = drm_atomic_helper_check,
Laurent Pinchart748471a52015-03-05 23:42:39 +0200205 .atomic_commit = omap_atomic_commit,
Rob Clarkcd5351f2011-11-12 12:09:40 -0600206};
207
208static int get_connector_type(struct omap_dss_device *dssdev)
209{
210 switch (dssdev->type) {
211 case OMAP_DISPLAY_TYPE_HDMI:
212 return DRM_MODE_CONNECTOR_HDMIA;
Tomi Valkeinen4635c172013-05-14 14:14:15 +0300213 case OMAP_DISPLAY_TYPE_DVI:
214 return DRM_MODE_CONNECTOR_DVID;
Sebastian Reichel4a64b902016-03-08 17:39:36 +0100215 case OMAP_DISPLAY_TYPE_DSI:
216 return DRM_MODE_CONNECTOR_DSI;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600217 default:
218 return DRM_MODE_CONNECTOR_Unknown;
219 }
220}
221
Archit Taneja0d8f3712013-03-26 19:15:19 +0530222static bool channel_used(struct drm_device *dev, enum omap_channel channel)
223{
224 struct omap_drm_private *priv = dev->dev_private;
225 int i;
226
227 for (i = 0; i < priv->num_crtcs; i++) {
228 struct drm_crtc *crtc = priv->crtcs[i];
229
230 if (omap_crtc_channel(crtc) == channel)
231 return true;
232 }
233
234 return false;
235}
Archit Tanejacc823bd2014-01-02 14:49:52 +0530236static void omap_disconnect_dssdevs(void)
237{
238 struct omap_dss_device *dssdev = NULL;
239
240 for_each_dss_dev(dssdev)
241 dssdev->driver->disconnect(dssdev);
242}
Archit Taneja0d8f3712013-03-26 19:15:19 +0530243
Archit Taneja3a01ab22014-01-02 14:49:51 +0530244static int omap_connect_dssdevs(void)
245{
246 int r;
247 struct omap_dss_device *dssdev = NULL;
Peter Ujfalusia09d2bc2016-05-03 22:08:01 +0300248
249 if (!omapdss_stack_is_ready())
250 return -EPROBE_DEFER;
Archit Taneja3a01ab22014-01-02 14:49:51 +0530251
252 for_each_dss_dev(dssdev) {
253 r = dssdev->driver->connect(dssdev);
254 if (r == -EPROBE_DEFER) {
255 omap_dss_put_device(dssdev);
256 goto cleanup;
257 } else if (r) {
258 dev_warn(dssdev->dev, "could not connect display: %s\n",
259 dssdev->name);
Archit Taneja3a01ab22014-01-02 14:49:51 +0530260 }
261 }
262
Archit Taneja3a01ab22014-01-02 14:49:51 +0530263 return 0;
264
265cleanup:
266 /*
267 * if we are deferring probe, we disconnect the devices we previously
268 * connected
269 */
Archit Tanejacc823bd2014-01-02 14:49:52 +0530270 omap_disconnect_dssdevs();
Archit Taneja3a01ab22014-01-02 14:49:51 +0530271
272 return r;
273}
Rob Clarkcd5351f2011-11-12 12:09:40 -0600274
Laurent Pinchartfb9a35f2015-01-11 16:30:44 +0200275static int omap_modeset_create_crtc(struct drm_device *dev, int id,
Tomi Valkeinene43f2c32016-12-02 16:07:11 +0200276 enum omap_channel channel,
277 u32 possible_crtcs)
Laurent Pinchartfb9a35f2015-01-11 16:30:44 +0200278{
279 struct omap_drm_private *priv = dev->dev_private;
280 struct drm_plane *plane;
281 struct drm_crtc *crtc;
282
Tomi Valkeinene43f2c32016-12-02 16:07:11 +0200283 plane = omap_plane_init(dev, id, DRM_PLANE_TYPE_PRIMARY,
284 possible_crtcs);
Laurent Pinchartfb9a35f2015-01-11 16:30:44 +0200285 if (IS_ERR(plane))
286 return PTR_ERR(plane);
287
288 crtc = omap_crtc_init(dev, plane, channel, id);
289
290 BUG_ON(priv->num_crtcs >= ARRAY_SIZE(priv->crtcs));
291 priv->crtcs[id] = crtc;
292 priv->num_crtcs++;
293
294 priv->planes[id] = plane;
295 priv->num_planes++;
296
297 return 0;
298}
299
Laurent Pincharte2cd09b2015-03-06 17:16:43 +0200300static int omap_modeset_init_properties(struct drm_device *dev)
301{
302 struct omap_drm_private *priv = dev->dev_private;
303
Laurent Pincharte2cd09b2015-03-06 17:16:43 +0200304 priv->zorder_prop = drm_property_create_range(dev, 0, "zorder", 0, 3);
305 if (!priv->zorder_prop)
306 return -ENOMEM;
307
308 return 0;
309}
310
Rob Clarkcd5351f2011-11-12 12:09:40 -0600311static int omap_modeset_init(struct drm_device *dev)
312{
Rob Clarkcd5351f2011-11-12 12:09:40 -0600313 struct omap_drm_private *priv = dev->dev_private;
314 struct omap_dss_device *dssdev = NULL;
Tomi Valkeinen9f759222015-11-05 18:39:52 +0200315 int num_ovls = priv->dispc_ops->get_num_ovls();
316 int num_mgrs = priv->dispc_ops->get_num_mgrs();
Jyri Sarhaf1118b82017-03-24 16:47:51 +0200317 int num_crtcs = 0;
Archit Taneja0d8f3712013-03-26 19:15:19 +0530318 int i, id = 0;
Laurent Pinchartfb9a35f2015-01-11 16:30:44 +0200319 int ret;
Tomi Valkeinene43f2c32016-12-02 16:07:11 +0200320 u32 possible_crtcs;
Tomi Valkeinen04b1fc02013-05-14 10:55:19 +0300321
Rob Clarkcd5351f2011-11-12 12:09:40 -0600322 drm_mode_config_init(dev);
323
Laurent Pincharte2cd09b2015-03-06 17:16:43 +0200324 ret = omap_modeset_init_properties(dev);
325 if (ret < 0)
326 return ret;
327
Rob Clarkf5f94542012-12-04 13:59:12 -0600328 /*
Jyri Sarhaf1118b82017-03-24 16:47:51 +0200329 * Let's create one CRTC for each connected DSS device if we
330 * have display managers and overlays (for primary planes) for
331 * them.
Rob Clarkf5f94542012-12-04 13:59:12 -0600332 */
Jyri Sarhaf1118b82017-03-24 16:47:51 +0200333 for_each_dss_dev(dssdev)
334 if (omapdss_device_is_connected(dssdev))
335 num_crtcs++;
336
337 num_crtcs = min3(num_crtcs, num_mgrs, num_ovls);
Tomi Valkeinene43f2c32016-12-02 16:07:11 +0200338 possible_crtcs = (1 << num_crtcs) - 1;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600339
Archit Taneja0d8f3712013-03-26 19:15:19 +0530340 dssdev = NULL;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600341
Rob Clarkf5f94542012-12-04 13:59:12 -0600342 for_each_dss_dev(dssdev) {
343 struct drm_connector *connector;
344 struct drm_encoder *encoder;
Archit Taneja0d8f3712013-03-26 19:15:19 +0530345 enum omap_channel channel;
Tomi Valkeinen179df152015-10-21 16:17:23 +0300346 struct omap_dss_device *out;
Rob Clarkf5f94542012-12-04 13:59:12 -0600347
Archit Taneja3a01ab22014-01-02 14:49:51 +0530348 if (!omapdss_device_is_connected(dssdev))
Archit Taneja581382e2013-03-26 19:15:18 +0530349 continue;
Tomi Valkeinena7e71e72013-05-08 16:23:32 +0300350
Rob Clarkf5f94542012-12-04 13:59:12 -0600351 encoder = omap_encoder_init(dev, dssdev);
352
353 if (!encoder) {
354 dev_err(dev->dev, "could not create encoder: %s\n",
355 dssdev->name);
356 return -ENOMEM;
357 }
358
359 connector = omap_connector_init(dev,
360 get_connector_type(dssdev), dssdev, encoder);
361
362 if (!connector) {
363 dev_err(dev->dev, "could not create connector: %s\n",
364 dssdev->name);
365 return -ENOMEM;
366 }
367
368 BUG_ON(priv->num_encoders >= ARRAY_SIZE(priv->encoders));
369 BUG_ON(priv->num_connectors >= ARRAY_SIZE(priv->connectors));
370
371 priv->encoders[priv->num_encoders++] = encoder;
372 priv->connectors[priv->num_connectors++] = connector;
373
374 drm_mode_connector_attach_encoder(connector, encoder);
375
Archit Taneja0d8f3712013-03-26 19:15:19 +0530376 /*
Jyri Sarhaf1118b82017-03-24 16:47:51 +0200377 * if we have reached the limit of the crtcs we can
378 * create, let's not try to create a crtc for this
379 * panel/encoder and onwards.
Archit Taneja0d8f3712013-03-26 19:15:19 +0530380 */
381 if (id == num_crtcs)
382 continue;
383
384 /*
385 * get the recommended DISPC channel for this encoder. For now,
386 * we only try to get create a crtc out of the recommended, the
387 * other possible channels to which the encoder can connect are
388 * not considered.
389 */
Archit Taneja0d8f3712013-03-26 19:15:19 +0530390
Tomi Valkeinen179df152015-10-21 16:17:23 +0300391 out = omapdss_find_output_from_display(dssdev);
392 channel = out->dispc_channel;
393 omap_dss_put_device(out);
394
Archit Taneja0d8f3712013-03-26 19:15:19 +0530395 /*
396 * if this channel hasn't already been taken by a previously
397 * allocated crtc, we create a new crtc for it
398 */
399 if (!channel_used(dev, channel)) {
Tomi Valkeinene43f2c32016-12-02 16:07:11 +0200400 ret = omap_modeset_create_crtc(dev, id, channel,
401 possible_crtcs);
Laurent Pinchartfb9a35f2015-01-11 16:30:44 +0200402 if (ret < 0) {
403 dev_err(dev->dev,
404 "could not create CRTC (channel %u)\n",
405 channel);
406 return ret;
407 }
Archit Taneja0d8f3712013-03-26 19:15:19 +0530408
409 id++;
410 }
411 }
412
413 /*
Archit Taneja0d8f3712013-03-26 19:15:19 +0530414 * Create normal planes for the remaining overlays:
415 */
416 for (; id < num_ovls; id++) {
Laurent Pinchartfb9a35f2015-01-11 16:30:44 +0200417 struct drm_plane *plane;
418
Tomi Valkeinene43f2c32016-12-02 16:07:11 +0200419 plane = omap_plane_init(dev, id, DRM_PLANE_TYPE_OVERLAY,
420 possible_crtcs);
Laurent Pinchartfb9a35f2015-01-11 16:30:44 +0200421 if (IS_ERR(plane))
422 return PTR_ERR(plane);
Archit Taneja0d8f3712013-03-26 19:15:19 +0530423
424 BUG_ON(priv->num_planes >= ARRAY_SIZE(priv->planes));
425 priv->planes[priv->num_planes++] = plane;
426 }
427
Jyri Sarhaf1118b82017-03-24 16:47:51 +0200428 /*
429 * populate the the possible_crtcs field for all the encoders
430 * we created.
431 */
Archit Taneja0d8f3712013-03-26 19:15:19 +0530432 for (i = 0; i < priv->num_encoders; i++) {
433 struct drm_encoder *encoder = priv->encoders[i];
434 struct omap_dss_device *dssdev =
435 omap_encoder_get_dssdev(encoder);
Tomi Valkeinen1f68d9c2013-04-19 15:09:34 +0300436 struct omap_dss_device *output;
Tomi Valkeinenbe8e8e12013-04-23 15:35:35 +0300437
438 output = omapdss_find_output_from_display(dssdev);
Archit Taneja0d8f3712013-03-26 19:15:19 +0530439
Rob Clarkf5f94542012-12-04 13:59:12 -0600440 /* figure out which crtc's we can connect the encoder to: */
441 encoder->possible_crtcs = 0;
442 for (id = 0; id < priv->num_crtcs; id++) {
Archit Taneja0d8f3712013-03-26 19:15:19 +0530443 struct drm_crtc *crtc = priv->crtcs[id];
444 enum omap_channel crtc_channel;
Archit Taneja0d8f3712013-03-26 19:15:19 +0530445
446 crtc_channel = omap_crtc_channel(crtc);
Archit Taneja0d8f3712013-03-26 19:15:19 +0530447
Tomi Valkeinen17337292014-09-03 19:25:49 +0000448 if (output->dispc_channel == crtc_channel) {
Rob Clarkf5f94542012-12-04 13:59:12 -0600449 encoder->possible_crtcs |= (1 << id);
Tomi Valkeinen17337292014-09-03 19:25:49 +0000450 break;
451 }
Rob Clarkf5f94542012-12-04 13:59:12 -0600452 }
Tomi Valkeinen820caab2013-04-25 14:53:18 +0300453
454 omap_dss_put_device(output);
Rob Clarkf5f94542012-12-04 13:59:12 -0600455 }
Rob Clarkcd5351f2011-11-12 12:09:40 -0600456
Archit Taneja0d8f3712013-03-26 19:15:19 +0530457 DBG("registered %d planes, %d crtcs, %d encoders and %d connectors\n",
458 priv->num_planes, priv->num_crtcs, priv->num_encoders,
459 priv->num_connectors);
460
Tomi Valkeinen1e907112016-08-23 12:35:39 +0300461 dev->mode_config.min_width = 8;
462 dev->mode_config.min_height = 2;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600463
464 /* note: eventually will need some cpu_is_omapXYZ() type stuff here
465 * to fill in these limits properly on different OMAP generations..
466 */
467 dev->mode_config.max_width = 2048;
468 dev->mode_config.max_height = 2048;
469
470 dev->mode_config.funcs = &omap_mode_config_funcs;
471
Laurent Pinchart69a12262015-03-05 21:38:16 +0200472 drm_mode_config_reset(dev);
473
Laurent Pinchart728ae8d2015-05-28 00:21:29 +0300474 omap_drm_irq_install(dev);
475
Rob Clarkcd5351f2011-11-12 12:09:40 -0600476 return 0;
477}
478
Rob Clarkcd5351f2011-11-12 12:09:40 -0600479/*
480 * drm ioctl funcs
481 */
482
483
484static int ioctl_get_param(struct drm_device *dev, void *data,
485 struct drm_file *file_priv)
486{
Rob Clark5e3b0872012-10-29 09:31:12 +0100487 struct omap_drm_private *priv = dev->dev_private;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600488 struct drm_omap_param *args = data;
489
490 DBG("%p: param=%llu", dev, args->param);
491
492 switch (args->param) {
493 case OMAP_PARAM_CHIPSET_ID:
Rob Clark5e3b0872012-10-29 09:31:12 +0100494 args->value = priv->omaprev;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600495 break;
496 default:
497 DBG("unknown parameter %lld", args->param);
498 return -EINVAL;
499 }
500
501 return 0;
502}
503
504static int ioctl_set_param(struct drm_device *dev, void *data,
505 struct drm_file *file_priv)
506{
507 struct drm_omap_param *args = data;
508
509 switch (args->param) {
510 default:
511 DBG("unknown parameter %lld", args->param);
512 return -EINVAL;
513 }
514
515 return 0;
516}
517
Laurent Pinchartef3f4e92015-12-14 22:39:36 +0200518#define OMAP_BO_USER_MASK 0x00ffffff /* flags settable by userspace */
519
Rob Clarkcd5351f2011-11-12 12:09:40 -0600520static int ioctl_gem_new(struct drm_device *dev, void *data,
521 struct drm_file *file_priv)
522{
523 struct drm_omap_gem_new *args = data;
Laurent Pinchartef3f4e92015-12-14 22:39:36 +0200524 u32 flags = args->flags & OMAP_BO_USER_MASK;
525
Rob Clarkf5f94542012-12-04 13:59:12 -0600526 VERB("%p:%p: size=0x%08x, flags=%08x", dev, file_priv,
Laurent Pinchartef3f4e92015-12-14 22:39:36 +0200527 args->size.bytes, flags);
528
529 return omap_gem_new_handle(dev, file_priv, args->size, flags,
530 &args->handle);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600531}
532
533static int ioctl_gem_cpu_prep(struct drm_device *dev, void *data,
534 struct drm_file *file_priv)
535{
536 struct drm_omap_gem_cpu_prep *args = data;
537 struct drm_gem_object *obj;
538 int ret;
539
540 VERB("%p:%p: handle=%d, op=%x", dev, file_priv, args->handle, args->op);
541
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100542 obj = drm_gem_object_lookup(file_priv, args->handle);
YAMANE Toshiakic7f904b2012-11-14 19:30:38 +0900543 if (!obj)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600544 return -ENOENT;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600545
546 ret = omap_gem_op_sync(obj, args->op);
547
YAMANE Toshiakic7f904b2012-11-14 19:30:38 +0900548 if (!ret)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600549 ret = omap_gem_op_start(obj, args->op);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600550
551 drm_gem_object_unreference_unlocked(obj);
552
553 return ret;
554}
555
556static int ioctl_gem_cpu_fini(struct drm_device *dev, void *data,
557 struct drm_file *file_priv)
558{
559 struct drm_omap_gem_cpu_fini *args = data;
560 struct drm_gem_object *obj;
561 int ret;
562
563 VERB("%p:%p: handle=%d", dev, file_priv, args->handle);
564
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100565 obj = drm_gem_object_lookup(file_priv, args->handle);
YAMANE Toshiakic7f904b2012-11-14 19:30:38 +0900566 if (!obj)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600567 return -ENOENT;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600568
569 /* XXX flushy, flushy */
570 ret = 0;
571
YAMANE Toshiakic7f904b2012-11-14 19:30:38 +0900572 if (!ret)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600573 ret = omap_gem_op_finish(obj, args->op);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600574
575 drm_gem_object_unreference_unlocked(obj);
576
577 return ret;
578}
579
580static int ioctl_gem_info(struct drm_device *dev, void *data,
581 struct drm_file *file_priv)
582{
583 struct drm_omap_gem_info *args = data;
584 struct drm_gem_object *obj;
585 int ret = 0;
586
Rob Clarkf5f94542012-12-04 13:59:12 -0600587 VERB("%p:%p: handle=%d", dev, file_priv, args->handle);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600588
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100589 obj = drm_gem_object_lookup(file_priv, args->handle);
YAMANE Toshiakic7f904b2012-11-14 19:30:38 +0900590 if (!obj)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600591 return -ENOENT;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600592
Rob Clarkf7f9f452011-12-05 19:19:22 -0600593 args->size = omap_gem_mmap_size(obj);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600594 args->offset = omap_gem_mmap_offset(obj);
595
596 drm_gem_object_unreference_unlocked(obj);
597
598 return ret;
599}
600
Rob Clarkbaa70942013-08-02 13:27:49 -0400601static const struct drm_ioctl_desc ioctls[DRM_COMMAND_END - DRM_COMMAND_BASE] = {
Hemant Hariyani5f6ab8c2016-06-07 13:23:19 -0500602 DRM_IOCTL_DEF_DRV(OMAP_GET_PARAM, ioctl_get_param,
603 DRM_AUTH | DRM_RENDER_ALLOW),
604 DRM_IOCTL_DEF_DRV(OMAP_SET_PARAM, ioctl_set_param,
605 DRM_AUTH | DRM_MASTER | DRM_ROOT_ONLY),
606 DRM_IOCTL_DEF_DRV(OMAP_GEM_NEW, ioctl_gem_new,
607 DRM_AUTH | DRM_RENDER_ALLOW),
608 DRM_IOCTL_DEF_DRV(OMAP_GEM_CPU_PREP, ioctl_gem_cpu_prep,
609 DRM_AUTH | DRM_RENDER_ALLOW),
610 DRM_IOCTL_DEF_DRV(OMAP_GEM_CPU_FINI, ioctl_gem_cpu_fini,
611 DRM_AUTH | DRM_RENDER_ALLOW),
612 DRM_IOCTL_DEF_DRV(OMAP_GEM_INFO, ioctl_gem_info,
613 DRM_AUTH | DRM_RENDER_ALLOW),
Rob Clarkcd5351f2011-11-12 12:09:40 -0600614};
615
616/*
617 * drm driver funcs
618 */
619
Rob Clarkcd5351f2011-11-12 12:09:40 -0600620static int dev_open(struct drm_device *dev, struct drm_file *file)
621{
622 file->driver_priv = NULL;
623
624 DBG("open: dev=%p, file=%p", dev, file);
625
626 return 0;
627}
628
Rob Clarkcd5351f2011-11-12 12:09:40 -0600629/**
630 * lastclose - clean up after all DRM clients have exited
631 * @dev: DRM device
632 *
633 * Take care of cleaning up after all DRM clients have exited. In the
634 * mode setting case, we want to restore the kernel's initial mode (just
635 * in case the last client left us in a bad state).
636 */
637static void dev_lastclose(struct drm_device *dev)
638{
Rob Clark3c810c62012-08-15 15:18:01 -0500639 int i;
640
Lukas Wunnerf15a66e2015-09-05 11:22:39 +0200641 /* we don't support vga_switcheroo.. so just make sure the fbdev
Rob Clarkcd5351f2011-11-12 12:09:40 -0600642 * mode is active
643 */
644 struct omap_drm_private *priv = dev->dev_private;
645 int ret;
646
647 DBG("lastclose: dev=%p", dev);
648
Ville Syrjälä0da88db2016-09-26 19:30:52 +0300649 /* need to restore default rotation state.. not sure
650 * if there is a cleaner way to restore properties to
651 * default state? Maybe a flag that properties should
652 * automatically be restored to default state on
653 * lastclose?
654 */
655 for (i = 0; i < priv->num_crtcs; i++) {
656 struct drm_crtc *crtc = priv->crtcs[i];
Rob Clark3c810c62012-08-15 15:18:01 -0500657
Ville Syrjälä0da88db2016-09-26 19:30:52 +0300658 if (!crtc->primary->rotation_property)
659 continue;
660
661 drm_object_property_set_value(&crtc->base,
662 crtc->primary->rotation_property,
663 DRM_ROTATE_0);
664 }
665
666 for (i = 0; i < priv->num_planes; i++) {
667 struct drm_plane *plane = priv->planes[i];
668
669 if (!plane->rotation_property)
670 continue;
671
672 drm_object_property_set_value(&plane->base,
673 plane->rotation_property,
674 DRM_ROTATE_0);
Rob Clark3c810c62012-08-15 15:18:01 -0500675 }
676
Tomi Valkeinenc7c1aec2014-09-25 19:24:26 +0000677 if (priv->fbdev) {
678 ret = drm_fb_helper_restore_fbdev_mode_unlocked(priv->fbdev);
679 if (ret)
680 DBG("failed to restore crtc mode");
681 }
Rob Clarkcd5351f2011-11-12 12:09:40 -0600682}
683
Laurent Pinchart78b68552012-05-17 13:27:22 +0200684static const struct vm_operations_struct omap_gem_vm_ops = {
Rob Clarkcd5351f2011-11-12 12:09:40 -0600685 .fault = omap_gem_fault,
686 .open = drm_gem_vm_open,
687 .close = drm_gem_vm_close,
688};
689
Rob Clarkff4f3872012-01-16 12:51:14 -0600690static const struct file_operations omapdriver_fops = {
Laurent Pinchart222025e2015-01-11 00:02:07 +0200691 .owner = THIS_MODULE,
692 .open = drm_open,
693 .unlocked_ioctl = drm_ioctl,
694 .release = drm_release,
695 .mmap = omap_gem_mmap,
696 .poll = drm_poll,
697 .read = drm_read,
698 .llseek = noop_llseek,
Rob Clarkff4f3872012-01-16 12:51:14 -0600699};
700
Rob Clarkcd5351f2011-11-12 12:09:40 -0600701static struct drm_driver omap_drm_driver = {
Tomi Valkeinen728fea72015-10-02 11:10:41 +0300702 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME |
Hemant Hariyani5f6ab8c2016-06-07 13:23:19 -0500703 DRIVER_ATOMIC | DRIVER_RENDER,
Laurent Pinchart222025e2015-01-11 00:02:07 +0200704 .open = dev_open,
705 .lastclose = dev_lastclose,
Andy Gross6169a1482011-12-15 21:05:17 -0600706#ifdef CONFIG_DEBUG_FS
Laurent Pinchart222025e2015-01-11 00:02:07 +0200707 .debugfs_init = omap_debugfs_init,
Andy Gross6169a1482011-12-15 21:05:17 -0600708#endif
Laurent Pinchart222025e2015-01-11 00:02:07 +0200709 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
710 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
711 .gem_prime_export = omap_gem_prime_export,
712 .gem_prime_import = omap_gem_prime_import,
713 .gem_free_object = omap_gem_free_object,
714 .gem_vm_ops = &omap_gem_vm_ops,
715 .dumb_create = omap_gem_dumb_create,
716 .dumb_map_offset = omap_gem_dumb_map_offset,
717 .dumb_destroy = drm_gem_dumb_destroy,
718 .ioctls = ioctls,
719 .num_ioctls = DRM_OMAP_NUM_IOCTLS,
720 .fops = &omapdriver_fops,
721 .name = DRIVER_NAME,
722 .desc = DRIVER_DESC,
723 .date = DRIVER_DATE,
724 .major = DRIVER_MAJOR,
725 .minor = DRIVER_MINOR,
726 .patchlevel = DRIVER_PATCHLEVEL,
Rob Clarkcd5351f2011-11-12 12:09:40 -0600727};
728
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200729static int pdev_probe(struct platform_device *pdev)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600730{
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200731 struct omap_drm_platform_data *pdata = pdev->dev.platform_data;
732 struct omap_drm_private *priv;
733 struct drm_device *ddev;
734 unsigned int i;
735 int ret;
736
737 DBG("%s", pdev->name);
Archit Taneja3a01ab22014-01-02 14:49:51 +0530738
Tomi Valkeinen591a0ac2013-05-23 12:07:50 +0300739 if (omapdss_is_initialized() == false)
740 return -EPROBE_DEFER;
741
Archit Taneja3a01ab22014-01-02 14:49:51 +0530742 omap_crtc_pre_init();
743
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200744 ret = omap_connect_dssdevs();
745 if (ret)
746 goto err_crtc_uninit;
747
748 /* Allocate and initialize the driver private structure. */
749 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
750 if (!priv) {
751 ret = -ENOMEM;
752 goto err_disconnect_dssdevs;
Archit Taneja3a01ab22014-01-02 14:49:51 +0530753 }
754
Tomi Valkeinen9f759222015-11-05 18:39:52 +0200755 priv->dispc_ops = dispc_get_ops();
756
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200757 priv->omaprev = pdata->omaprev;
758 priv->wq = alloc_ordered_workqueue("omapdrm", 0);
759
760 init_waitqueue_head(&priv->commit.wait);
761 spin_lock_init(&priv->commit.lock);
762 spin_lock_init(&priv->list_lock);
763 INIT_LIST_HEAD(&priv->obj_list);
764
765 /* Allocate and initialize the DRM device. */
766 ddev = drm_dev_alloc(&omap_drm_driver, &pdev->dev);
767 if (IS_ERR(ddev)) {
768 ret = PTR_ERR(ddev);
769 goto err_free_priv;
770 }
771
772 ddev->dev_private = priv;
773 platform_set_drvdata(pdev, ddev);
774
775 omap_gem_init(ddev);
776
777 ret = omap_modeset_init(ddev);
778 if (ret) {
779 dev_err(&pdev->dev, "omap_modeset_init failed: ret=%d\n", ret);
780 goto err_free_drm_dev;
781 }
782
783 /* Initialize vblank handling, start with all CRTCs disabled. */
784 ret = drm_vblank_init(ddev, priv->num_crtcs);
785 if (ret) {
786 dev_err(&pdev->dev, "could not init vblank\n");
787 goto err_cleanup_modeset;
788 }
789
790 for (i = 0; i < priv->num_crtcs; i++)
791 drm_crtc_vblank_off(priv->crtcs[i]);
792
793 priv->fbdev = omap_fbdev_init(ddev);
794
795 drm_kms_helper_poll_init(ddev);
796
797 /*
798 * Register the DRM device with the core and the connectors with
799 * sysfs.
800 */
801 ret = drm_dev_register(ddev, 0);
802 if (ret)
803 goto err_cleanup_helpers;
804
805 return 0;
806
807err_cleanup_helpers:
808 drm_kms_helper_poll_fini(ddev);
809 if (priv->fbdev)
810 omap_fbdev_free(ddev);
811err_cleanup_modeset:
812 drm_mode_config_cleanup(ddev);
813 omap_drm_irq_uninstall(ddev);
814err_free_drm_dev:
815 omap_gem_deinit(ddev);
816 drm_dev_unref(ddev);
817err_free_priv:
818 destroy_workqueue(priv->wq);
819 kfree(priv);
820err_disconnect_dssdevs:
821 omap_disconnect_dssdevs();
822err_crtc_uninit:
823 omap_crtc_pre_uninit();
824 return ret;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600825}
826
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200827static int pdev_remove(struct platform_device *pdev)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600828{
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200829 struct drm_device *ddev = platform_get_drvdata(pdev);
830 struct omap_drm_private *priv = ddev->dev_private;
831
Rob Clarkcd5351f2011-11-12 12:09:40 -0600832 DBG("");
Andy Gross5c137792012-03-05 10:48:39 -0600833
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200834 drm_dev_unregister(ddev);
835
836 drm_kms_helper_poll_fini(ddev);
837
838 if (priv->fbdev)
839 omap_fbdev_free(ddev);
840
Tomi Valkeinen8a54aa92017-03-27 10:02:22 +0300841 drm_atomic_helper_shutdown(ddev);
842
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200843 drm_mode_config_cleanup(ddev);
844
845 omap_drm_irq_uninstall(ddev);
846 omap_gem_deinit(ddev);
847
848 drm_dev_unref(ddev);
849
850 destroy_workqueue(priv->wq);
851 kfree(priv);
Tomi Valkeinen707cf582014-04-02 13:47:43 +0300852
Archit Tanejacc823bd2014-01-02 14:49:52 +0530853 omap_disconnect_dssdevs();
854 omap_crtc_pre_uninit();
Daniel Vetterfd3c0252013-12-11 11:34:26 +0100855
Rob Clarkcd5351f2011-11-12 12:09:40 -0600856 return 0;
857}
858
Grygorii Strashko8450c8d2015-02-26 15:57:17 +0200859#ifdef CONFIG_PM_SLEEP
Tomi Valkeinen92bf0f92015-10-02 11:10:42 +0300860static int omap_drm_suspend_all_displays(void)
861{
862 struct omap_dss_device *dssdev = NULL;
863
864 for_each_dss_dev(dssdev) {
865 if (!dssdev->driver)
866 continue;
867
868 if (dssdev->state == OMAP_DSS_DISPLAY_ACTIVE) {
869 dssdev->driver->disable(dssdev);
870 dssdev->activate_after_resume = true;
871 } else {
872 dssdev->activate_after_resume = false;
873 }
874 }
875
876 return 0;
877}
878
879static int omap_drm_resume_all_displays(void)
880{
881 struct omap_dss_device *dssdev = NULL;
882
883 for_each_dss_dev(dssdev) {
884 if (!dssdev->driver)
885 continue;
886
887 if (dssdev->activate_after_resume) {
888 dssdev->driver->enable(dssdev);
889 dssdev->activate_after_resume = false;
890 }
891 }
892
893 return 0;
894}
895
Tomi Valkeinenccd7b5e2014-11-14 15:18:28 +0200896static int omap_drm_suspend(struct device *dev)
897{
898 struct drm_device *drm_dev = dev_get_drvdata(dev);
899
900 drm_kms_helper_poll_disable(drm_dev);
901
Tomi Valkeinen92bf0f92015-10-02 11:10:42 +0300902 drm_modeset_lock_all(drm_dev);
903 omap_drm_suspend_all_displays();
904 drm_modeset_unlock_all(drm_dev);
905
Tomi Valkeinenccd7b5e2014-11-14 15:18:28 +0200906 return 0;
907}
908
909static int omap_drm_resume(struct device *dev)
910{
911 struct drm_device *drm_dev = dev_get_drvdata(dev);
912
Tomi Valkeinen92bf0f92015-10-02 11:10:42 +0300913 drm_modeset_lock_all(drm_dev);
914 omap_drm_resume_all_displays();
915 drm_modeset_unlock_all(drm_dev);
916
Tomi Valkeinenccd7b5e2014-11-14 15:18:28 +0200917 drm_kms_helper_poll_enable(drm_dev);
918
919 return omap_gem_resume(dev);
920}
Andy Grosse78edba2012-12-19 14:53:37 -0600921#endif
922
Grygorii Strashko8450c8d2015-02-26 15:57:17 +0200923static SIMPLE_DEV_PM_OPS(omapdrm_pm_ops, omap_drm_suspend, omap_drm_resume);
924
Tomi Valkeinen6717cd22013-04-10 10:44:00 +0300925static struct platform_driver pdev = {
Laurent Pinchart222025e2015-01-11 00:02:07 +0200926 .driver = {
927 .name = DRIVER_NAME,
Laurent Pinchart222025e2015-01-11 00:02:07 +0200928 .pm = &omapdrm_pm_ops,
Laurent Pinchart222025e2015-01-11 00:02:07 +0200929 },
930 .probe = pdev_probe,
931 .remove = pdev_remove,
Rob Clarkcd5351f2011-11-12 12:09:40 -0600932};
933
Thierry Redinge1c49bd2015-12-02 17:23:31 +0100934static struct platform_driver * const drivers[] = {
935 &omap_dmm_driver,
936 &pdev,
937};
938
Rob Clarkcd5351f2011-11-12 12:09:40 -0600939static int __init omap_drm_init(void)
940{
941 DBG("init");
Tomi Valkeinenea7e3a62014-04-02 14:31:50 +0300942
Thierry Redinge1c49bd2015-12-02 17:23:31 +0100943 return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
Rob Clarkcd5351f2011-11-12 12:09:40 -0600944}
945
946static void __exit omap_drm_fini(void)
947{
948 DBG("fini");
Tomi Valkeinenea7e3a62014-04-02 14:31:50 +0300949
Thierry Redinge1c49bd2015-12-02 17:23:31 +0100950 platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
Rob Clarkcd5351f2011-11-12 12:09:40 -0600951}
952
953/* need late_initcall() so we load after dss_driver's are loaded */
954late_initcall(omap_drm_init);
955module_exit(omap_drm_fini);
956
957MODULE_AUTHOR("Rob Clark <rob@ti.com>");
958MODULE_DESCRIPTION("OMAP DRM Display Driver");
959MODULE_ALIAS("platform:" DRIVER_NAME);
960MODULE_LICENSE("GPL v2");