blob: 1b9b6f5e48e17e5e8ecdd17a6446612e2dc5d906 [file] [log] [blame]
Rob Clarkcd5351f2011-11-12 12:09:40 -06001/*
Andrew F. Davisbb5cdf82017-12-05 14:29:31 -06002 * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
Rob Clarkcd5351f2011-11-12 12:09:40 -06003 * Author: Rob Clark <rob@ti.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
Peter Ujfalusieb5bc1f2018-02-12 11:44:39 +020018#include <linux/of.h>
19#include <linux/sort.h>
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_fb_helper.h>
Daniel Vetterfcd70cd2019-01-17 22:03:34 +010025#include <drm/drm_probe_helper.h>
Laurent Pinchart30b71762018-12-07 23:08:35 +020026#include <drm/drm_panel.h>
Rob Clarkcd5351f2011-11-12 12:09:40 -060027
Andy Gross5c137792012-03-05 10:48:39 -060028#include "omap_dmm_tiler.h"
Laurent Pinchart2d278f52015-03-05 21:31:37 +020029#include "omap_drv.h"
Rob Clarkcd5351f2011-11-12 12:09:40 -060030
31#define DRIVER_NAME MODULE_NAME
32#define DRIVER_DESC "OMAP DRM"
33#define DRIVER_DATE "20110917"
34#define DRIVER_MAJOR 1
35#define DRIVER_MINOR 0
36#define DRIVER_PATCHLEVEL 0
37
Rob Clarkcd5351f2011-11-12 12:09:40 -060038/*
39 * mode config funcs
40 */
41
42/* Notes about mapping DSS and DRM entities:
43 * CRTC: overlay
44 * encoder: manager.. with some extension to allow one primary CRTC
45 * and zero or more video CRTC's to be mapped to one encoder?
46 * connector: dssdev.. manager can be attached/detached from different
47 * devices
48 */
49
Tomi Valkeinen5f741b32015-05-29 16:01:18 +030050static void omap_atomic_wait_for_completion(struct drm_device *dev,
51 struct drm_atomic_state *old_state)
52{
Maarten Lankhorst34d88232017-07-19 16:39:17 +020053 struct drm_crtc_state *new_crtc_state;
Tomi Valkeinen5f741b32015-05-29 16:01:18 +030054 struct drm_crtc *crtc;
55 unsigned int i;
56 int ret;
57
Maarten Lankhorst34d88232017-07-19 16:39:17 +020058 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
59 if (!new_crtc_state->active)
Tomi Valkeinen5f741b32015-05-29 16:01:18 +030060 continue;
61
62 ret = omap_crtc_wait_pending(crtc);
63
64 if (!ret)
65 dev_warn(dev->dev,
66 "atomic complete timeout (pipe %u)!\n", i);
67 }
68}
69
Laurent Pincharta9e6f9f2017-05-09 01:27:10 +030070static void omap_atomic_commit_tail(struct drm_atomic_state *old_state)
Laurent Pinchart748471a52015-03-05 23:42:39 +020071{
Laurent Pincharta9e6f9f2017-05-09 01:27:10 +030072 struct drm_device *dev = old_state->dev;
Laurent Pinchart748471a52015-03-05 23:42:39 +020073 struct omap_drm_private *priv = dev->dev_private;
Laurent Pinchart748471a52015-03-05 23:42:39 +020074
Laurent Pinchart50638ae2018-02-13 14:00:42 +020075 priv->dispc_ops->runtime_get(priv->dispc);
Laurent Pinchart69fb7c82015-05-28 02:09:56 +030076
Laurent Pincharta9e6f9f2017-05-09 01:27:10 +030077 /* Apply the atomic update. */
Laurent Pinchart748471a52015-03-05 23:42:39 +020078 drm_atomic_helper_commit_modeset_disables(dev, old_state);
Jyri Sarha897145d2017-01-27 12:04:55 +020079
Tomi Valkeinenfc5cc9672017-08-23 12:19:02 +030080 if (priv->omaprev != 0x3430) {
81 /* With the current dss dispc implementation we have to enable
82 * the new modeset before we can commit planes. The dispc ovl
83 * configuration relies on the video mode configuration been
84 * written into the HW when the ovl configuration is
85 * calculated.
86 *
87 * This approach is not ideal because after a mode change the
88 * plane update is executed only after the first vblank
89 * interrupt. The dispc implementation should be fixed so that
90 * it is able use uncommitted drm state information.
91 */
92 drm_atomic_helper_commit_modeset_enables(dev, old_state);
93 omap_atomic_wait_for_completion(dev, old_state);
Jyri Sarha897145d2017-01-27 12:04:55 +020094
Tomi Valkeinenfc5cc9672017-08-23 12:19:02 +030095 drm_atomic_helper_commit_planes(dev, old_state, 0);
Laurent Pinchart748471a52015-03-05 23:42:39 +020096
Tomi Valkeinenfc5cc9672017-08-23 12:19:02 +030097 drm_atomic_helper_commit_hw_done(old_state);
98 } else {
99 /*
100 * OMAP3 DSS seems to have issues with the work-around above,
101 * resulting in endless sync losts if a crtc is enabled without
102 * a plane. For now, skip the WA for OMAP3.
103 */
104 drm_atomic_helper_commit_planes(dev, old_state, 0);
105
106 drm_atomic_helper_commit_modeset_enables(dev, old_state);
107
108 drm_atomic_helper_commit_hw_done(old_state);
109 }
Laurent Pincharta9e6f9f2017-05-09 01:27:10 +0300110
111 /*
112 * Wait for completion of the page flips to ensure that old buffers
113 * can't be touched by the hardware anymore before cleaning up planes.
114 */
Tomi Valkeinen5f741b32015-05-29 16:01:18 +0300115 omap_atomic_wait_for_completion(dev, old_state);
Laurent Pinchart748471a52015-03-05 23:42:39 +0200116
117 drm_atomic_helper_cleanup_planes(dev, old_state);
118
Laurent Pinchart50638ae2018-02-13 14:00:42 +0200119 priv->dispc_ops->runtime_put(priv->dispc);
Laurent Pinchart748471a52015-03-05 23:42:39 +0200120}
121
Laurent Pincharta9e6f9f2017-05-09 01:27:10 +0300122static const struct drm_mode_config_helper_funcs omap_mode_config_helper_funcs = {
123 .atomic_commit_tail = omap_atomic_commit_tail,
124};
Laurent Pinchart748471a52015-03-05 23:42:39 +0200125
Laurent Pincharte6ecefa2012-05-17 13:27:23 +0200126static const struct drm_mode_config_funcs omap_mode_config_funcs = {
Rob Clarkcd5351f2011-11-12 12:09:40 -0600127 .fb_create = omap_framebuffer_create,
Noralf Trønnesef62d302017-12-05 19:25:01 +0100128 .output_poll_changed = drm_fb_helper_output_poll_changed,
Laurent Pinchartcef77d42015-03-05 21:50:00 +0200129 .atomic_check = drm_atomic_helper_check,
Laurent Pincharta9e6f9f2017-05-09 01:27:10 +0300130 .atomic_commit = drm_atomic_helper_commit,
Rob Clarkcd5351f2011-11-12 12:09:40 -0600131};
132
Laurent Pinchart2ee76792018-03-05 15:02:22 +0200133static void omap_disconnect_pipelines(struct drm_device *ddev)
Archit Tanejacc823bd2014-01-02 14:49:52 +0530134{
Peter Ujfalusi52b9ef22018-02-12 11:44:37 +0200135 struct omap_drm_private *priv = ddev->dev_private;
136 unsigned int i;
Archit Tanejacc823bd2014-01-02 14:49:52 +0530137
Laurent Pinchart2ee76792018-03-05 15:02:22 +0200138 for (i = 0; i < priv->num_pipes; i++) {
Laurent Pinchart511afb42018-03-04 23:42:36 +0200139 struct omap_drm_pipeline *pipe = &priv->pipes[i];
Peter Ujfalusi52b9ef22018-02-12 11:44:37 +0200140
Laurent Pinchart30b71762018-12-07 23:08:35 +0200141 if (pipe->output->panel)
142 drm_panel_detach(pipe->output->panel);
143
Laurent Pinchart511afb42018-03-04 23:42:36 +0200144 omapdss_device_disconnect(NULL, pipe->output);
145
146 omapdss_device_put(pipe->output);
Laurent Pinchart511afb42018-03-04 23:42:36 +0200147 pipe->output = NULL;
Peter Ujfalusi52b9ef22018-02-12 11:44:37 +0200148 }
149
Laurent Pincharte48f9f12018-03-07 00:01:33 +0200150 memset(&priv->channels, 0, sizeof(priv->channels));
151
Laurent Pinchart2ee76792018-03-05 15:02:22 +0200152 priv->num_pipes = 0;
Archit Tanejacc823bd2014-01-02 14:49:52 +0530153}
Archit Taneja0d8f3712013-03-26 19:15:19 +0530154
Laurent Pinchart2ee76792018-03-05 15:02:22 +0200155static int omap_connect_pipelines(struct drm_device *ddev)
Archit Taneja3a01ab22014-01-02 14:49:51 +0530156{
Peter Ujfalusi52b9ef22018-02-12 11:44:37 +0200157 struct omap_drm_private *priv = ddev->dev_private;
Laurent Pinchart511afb42018-03-04 23:42:36 +0200158 struct omap_dss_device *output = NULL;
Peter Ujfalusi52b9ef22018-02-12 11:44:37 +0200159 int r;
Peter Ujfalusia09d2bc2016-05-03 22:08:01 +0300160
Laurent Pinchart511afb42018-03-04 23:42:36 +0200161 for_each_dss_output(output) {
162 r = omapdss_device_connect(priv->dss, NULL, output);
Archit Taneja3a01ab22014-01-02 14:49:51 +0530163 if (r == -EPROBE_DEFER) {
Laurent Pinchart511afb42018-03-04 23:42:36 +0200164 omapdss_device_put(output);
Laurent Pincharta4e26522018-09-23 14:13:15 +0300165 return r;
Archit Taneja3a01ab22014-01-02 14:49:51 +0530166 } else if (r) {
Laurent Pinchart511afb42018-03-04 23:42:36 +0200167 dev_warn(output->dev, "could not connect output %s\n",
168 output->name);
Peter Ujfalusi52b9ef22018-02-12 11:44:37 +0200169 } else {
Laurent Pinchart511afb42018-03-04 23:42:36 +0200170 struct omap_drm_pipeline *pipe;
171
172 pipe = &priv->pipes[priv->num_pipes++];
173 pipe->output = omapdss_device_get(output);
Laurent Pinchart511afb42018-03-04 23:42:36 +0200174
Laurent Pinchart2ee76792018-03-05 15:02:22 +0200175 if (priv->num_pipes == ARRAY_SIZE(priv->pipes)) {
Laurent Pinchart511afb42018-03-04 23:42:36 +0200176 /* To balance the 'for_each_dss_output' loop */
177 omapdss_device_put(output);
Peter Ujfalusi52b9ef22018-02-12 11:44:37 +0200178 break;
179 }
Archit Taneja3a01ab22014-01-02 14:49:51 +0530180 }
181 }
182
Archit Taneja3a01ab22014-01-02 14:49:51 +0530183 return 0;
Laurent Pincharta4e26522018-09-23 14:13:15 +0300184}
Archit Taneja3a01ab22014-01-02 14:49:51 +0530185
Laurent Pincharta4e26522018-09-23 14:13:15 +0300186static int omap_compare_pipelines(const void *a, const void *b)
187{
188 const struct omap_drm_pipeline *pipe1 = a;
189 const struct omap_drm_pipeline *pipe2 = b;
Archit Taneja3a01ab22014-01-02 14:49:51 +0530190
Laurent Pincharta4e26522018-09-23 14:13:15 +0300191 if (pipe1->alias_id > pipe2->alias_id)
192 return 1;
193 else if (pipe1->alias_id < pipe2->alias_id)
194 return -1;
195 return 0;
Archit Taneja3a01ab22014-01-02 14:49:51 +0530196}
Rob Clarkcd5351f2011-11-12 12:09:40 -0600197
Laurent Pincharte2cd09b2015-03-06 17:16:43 +0200198static int omap_modeset_init_properties(struct drm_device *dev)
199{
200 struct omap_drm_private *priv = dev->dev_private;
Laurent Pinchart50638ae2018-02-13 14:00:42 +0200201 unsigned int num_planes = priv->dispc_ops->get_num_ovls(priv->dispc);
Laurent Pincharte2cd09b2015-03-06 17:16:43 +0200202
Laurent Pinchartdff6c242017-05-09 01:27:14 +0300203 priv->zorder_prop = drm_property_create_range(dev, 0, "zorder", 0,
204 num_planes - 1);
Laurent Pincharte2cd09b2015-03-06 17:16:43 +0200205 if (!priv->zorder_prop)
206 return -ENOMEM;
207
208 return 0;
209}
210
Laurent Pinchart79107f22018-09-23 12:58:15 +0300211static int omap_display_id(struct omap_dss_device *output)
212{
213 struct device_node *node = NULL;
214
215 if (output->next) {
216 struct omap_dss_device *display;
217
218 display = omapdss_display_get(output);
219 node = display->dev->of_node;
220 omapdss_device_put(display);
Laurent Pinchart30b71762018-12-07 23:08:35 +0200221 } else if (output->bridge) {
Laurent Pinchart79107f22018-09-23 12:58:15 +0300222 struct drm_bridge *bridge = output->bridge;
223
224 while (bridge->next)
225 bridge = bridge->next;
226
227 node = bridge->of_node;
Laurent Pinchart30b71762018-12-07 23:08:35 +0200228 } else if (output->panel) {
229 node = output->panel->dev->of_node;
Laurent Pinchart79107f22018-09-23 12:58:15 +0300230 }
231
232 return node ? of_alias_get_id(node, "display") : -ENODEV;
233}
234
Rob Clarkcd5351f2011-11-12 12:09:40 -0600235static int omap_modeset_init(struct drm_device *dev)
236{
Rob Clarkcd5351f2011-11-12 12:09:40 -0600237 struct omap_drm_private *priv = dev->dev_private;
Laurent Pinchart50638ae2018-02-13 14:00:42 +0200238 int num_ovls = priv->dispc_ops->get_num_ovls(priv->dispc);
239 int num_mgrs = priv->dispc_ops->get_num_mgrs(priv->dispc);
Peter Ujfalusi52b9ef22018-02-12 11:44:37 +0200240 unsigned int i;
Laurent Pinchartfb9a35f2015-01-11 16:30:44 +0200241 int ret;
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200242 u32 plane_crtc_mask;
Tomi Valkeinen04b1fc02013-05-14 10:55:19 +0300243
Laurent Pincharta4e26522018-09-23 14:13:15 +0300244 if (!omapdss_stack_is_ready())
245 return -EPROBE_DEFER;
246
Rob Clarkcd5351f2011-11-12 12:09:40 -0600247 drm_mode_config_init(dev);
248
Laurent Pincharte2cd09b2015-03-06 17:16:43 +0200249 ret = omap_modeset_init_properties(dev);
250 if (ret < 0)
251 return ret;
252
Rob Clarkf5f94542012-12-04 13:59:12 -0600253 /*
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200254 * This function creates exactly one connector, encoder, crtc,
255 * and primary plane per each connected dss-device. Each
256 * connector->encoder->crtc chain is expected to be separate
257 * and each crtc is connect to a single dss-channel. If the
258 * configuration does not match the expectations or exceeds
259 * the available resources, the configuration is rejected.
Rob Clarkf5f94542012-12-04 13:59:12 -0600260 */
Laurent Pincharta4e26522018-09-23 14:13:15 +0300261 ret = omap_connect_pipelines(dev);
262 if (ret < 0)
263 return ret;
264
Laurent Pinchart2ee76792018-03-05 15:02:22 +0200265 if (priv->num_pipes > num_mgrs || priv->num_pipes > num_ovls) {
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200266 dev_err(dev->dev, "%s(): Too many connected displays\n",
267 __func__);
268 return -EINVAL;
269 }
270
Laurent Pinchartac3b1312018-03-05 19:11:30 +0200271 /* Create all planes first. They can all be put to any CRTC. */
Laurent Pinchart2ee76792018-03-05 15:02:22 +0200272 plane_crtc_mask = (1 << priv->num_pipes) - 1;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600273
Laurent Pinchartac3b1312018-03-05 19:11:30 +0200274 for (i = 0; i < num_ovls; i++) {
Laurent Pinchart2ee76792018-03-05 15:02:22 +0200275 enum drm_plane_type type = i < priv->num_pipes
Laurent Pinchartac3b1312018-03-05 19:11:30 +0200276 ? DRM_PLANE_TYPE_PRIMARY
277 : DRM_PLANE_TYPE_OVERLAY;
278 struct drm_plane *plane;
279
280 if (WARN_ON(priv->num_planes >= ARRAY_SIZE(priv->planes)))
281 return -EINVAL;
282
283 plane = omap_plane_init(dev, i, type, plane_crtc_mask);
284 if (IS_ERR(plane))
285 return PTR_ERR(plane);
286
287 priv->planes[priv->num_planes++] = plane;
288 }
289
Laurent Pinchart79107f22018-09-23 12:58:15 +0300290 /*
291 * Create the encoders, attach the bridges and get the pipeline alias
292 * IDs.
293 */
Laurent Pinchart2ee76792018-03-05 15:02:22 +0200294 for (i = 0; i < priv->num_pipes; i++) {
295 struct omap_drm_pipeline *pipe = &priv->pipes[i];
Laurent Pincharta4e26522018-09-23 14:13:15 +0300296 int id;
297
298 pipe->encoder = omap_encoder_init(dev, pipe->output);
299 if (!pipe->encoder)
300 return -ENOMEM;
301
Laurent Pinchart79107f22018-09-23 12:58:15 +0300302 if (pipe->output->bridge) {
303 ret = drm_bridge_attach(pipe->encoder,
304 pipe->output->bridge, NULL);
305 if (ret < 0)
306 return ret;
307 }
308
309 id = omap_display_id(pipe->output);
Laurent Pincharta4e26522018-09-23 14:13:15 +0300310 pipe->alias_id = id >= 0 ? id : i;
311 }
312
313 /* Sort the pipelines by DT aliases. */
314 sort(priv->pipes, priv->num_pipes, sizeof(priv->pipes[0]),
315 omap_compare_pipelines, NULL);
316
317 /*
318 * Populate the pipeline lookup table by DISPC channel. Only one display
319 * is allowed per channel.
320 */
321 for (i = 0; i < priv->num_pipes; ++i) {
322 struct omap_drm_pipeline *pipe = &priv->pipes[i];
323 enum omap_channel channel = pipe->output->dispc_channel;
324
325 if (WARN_ON(priv->channels[channel] != NULL))
326 return -EINVAL;
327
328 priv->channels[channel] = pipe;
329 }
330
331 /* Create the connectors and CRTCs. */
332 for (i = 0; i < priv->num_pipes; i++) {
333 struct omap_drm_pipeline *pipe = &priv->pipes[i];
334 struct drm_encoder *encoder = pipe->encoder;
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200335 struct drm_crtc *crtc;
Rob Clarkf5f94542012-12-04 13:59:12 -0600336
Laurent Pinchart79107f22018-09-23 12:58:15 +0300337 if (!pipe->output->bridge) {
338 pipe->connector = omap_connector_init(dev, pipe->output,
339 encoder);
340 if (!pipe->connector)
341 return -ENOMEM;
Rob Clarkf5f94542012-12-04 13:59:12 -0600342
Laurent Pinchart79107f22018-09-23 12:58:15 +0300343 drm_connector_attach_encoder(pipe->connector, encoder);
Laurent Pinchart30b71762018-12-07 23:08:35 +0200344
345 if (pipe->output->panel) {
346 ret = drm_panel_attach(pipe->output->panel,
347 pipe->connector);
348 if (ret < 0)
349 return ret;
350 }
Laurent Pinchart79107f22018-09-23 12:58:15 +0300351 }
Laurent Pincharta4e26522018-09-23 14:13:15 +0300352
Laurent Pinchart00b30e72018-03-06 23:37:25 +0200353 crtc = omap_crtc_init(dev, pipe, priv->planes[i]);
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200354 if (IS_ERR(crtc))
355 return PTR_ERR(crtc);
356
Laurent Pinchartf9699362018-03-05 14:47:47 +0200357 encoder->possible_crtcs = 1 << i;
Laurent Pinchart2ee76792018-03-05 15:02:22 +0200358 pipe->crtc = crtc;
Archit Taneja0d8f3712013-03-26 19:15:19 +0530359 }
360
Laurent Pinchart2ee76792018-03-05 15:02:22 +0200361 DBG("registered %u planes, %u crtcs/encoders/connectors\n",
362 priv->num_planes, priv->num_pipes);
Archit Taneja0d8f3712013-03-26 19:15:19 +0530363
Tomi Valkeinen1e907112016-08-23 12:35:39 +0300364 dev->mode_config.min_width = 8;
365 dev->mode_config.min_height = 2;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600366
Tomi Valkeinen1915d7f2018-01-10 11:31:18 +0200367 /*
368 * Note: these values are used for multiple independent things:
369 * connector mode filtering, buffer sizes, crtc sizes...
370 * Use big enough values here to cover all use cases, and do more
371 * specific checking in the respective code paths.
Rob Clarkcd5351f2011-11-12 12:09:40 -0600372 */
Tomi Valkeinen1915d7f2018-01-10 11:31:18 +0200373 dev->mode_config.max_width = 8192;
374 dev->mode_config.max_height = 8192;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600375
Peter Ujfalusi23936ba2018-03-21 12:20:29 +0200376 /* We want the zpos to be normalized */
377 dev->mode_config.normalize_zpos = true;
378
Rob Clarkcd5351f2011-11-12 12:09:40 -0600379 dev->mode_config.funcs = &omap_mode_config_funcs;
Laurent Pincharta9e6f9f2017-05-09 01:27:10 +0300380 dev->mode_config.helper_private = &omap_mode_config_helper_funcs;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600381
Laurent Pinchart69a12262015-03-05 21:38:16 +0200382 drm_mode_config_reset(dev);
383
Laurent Pinchart728ae8d2015-05-28 00:21:29 +0300384 omap_drm_irq_install(dev);
385
Rob Clarkcd5351f2011-11-12 12:09:40 -0600386 return 0;
387}
388
Rob Clarkcd5351f2011-11-12 12:09:40 -0600389/*
Peter Ujfalusi3c596802017-06-02 15:26:35 +0300390 * Enable the HPD in external components if supported
391 */
Peter Ujfalusi52b9ef22018-02-12 11:44:37 +0200392static void omap_modeset_enable_external_hpd(struct drm_device *ddev)
Peter Ujfalusi3c596802017-06-02 15:26:35 +0300393{
Peter Ujfalusi52b9ef22018-02-12 11:44:37 +0200394 struct omap_drm_private *priv = ddev->dev_private;
Laurent Pinchart79107f22018-09-23 12:58:15 +0300395 unsigned int i;
Peter Ujfalusi3c596802017-06-02 15:26:35 +0300396
Laurent Pinchart79107f22018-09-23 12:58:15 +0300397 for (i = 0; i < priv->num_pipes; i++) {
398 if (priv->pipes[i].connector)
399 omap_connector_enable_hpd(priv->pipes[i].connector);
400 }
Peter Ujfalusi3c596802017-06-02 15:26:35 +0300401}
402
403/*
404 * Disable the HPD in external components if supported
405 */
Peter Ujfalusi52b9ef22018-02-12 11:44:37 +0200406static void omap_modeset_disable_external_hpd(struct drm_device *ddev)
Peter Ujfalusi3c596802017-06-02 15:26:35 +0300407{
Peter Ujfalusi52b9ef22018-02-12 11:44:37 +0200408 struct omap_drm_private *priv = ddev->dev_private;
Laurent Pinchart79107f22018-09-23 12:58:15 +0300409 unsigned int i;
Peter Ujfalusi3c596802017-06-02 15:26:35 +0300410
Laurent Pinchart79107f22018-09-23 12:58:15 +0300411 for (i = 0; i < priv->num_pipes; i++) {
412 if (priv->pipes[i].connector)
413 omap_connector_disable_hpd(priv->pipes[i].connector);
414 }
Peter Ujfalusi3c596802017-06-02 15:26:35 +0300415}
416
417/*
Rob Clarkcd5351f2011-11-12 12:09:40 -0600418 * drm ioctl funcs
419 */
420
421
422static int ioctl_get_param(struct drm_device *dev, void *data,
423 struct drm_file *file_priv)
424{
Rob Clark5e3b0872012-10-29 09:31:12 +0100425 struct omap_drm_private *priv = dev->dev_private;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600426 struct drm_omap_param *args = data;
427
428 DBG("%p: param=%llu", dev, args->param);
429
430 switch (args->param) {
431 case OMAP_PARAM_CHIPSET_ID:
Rob Clark5e3b0872012-10-29 09:31:12 +0100432 args->value = priv->omaprev;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600433 break;
434 default:
435 DBG("unknown parameter %lld", args->param);
436 return -EINVAL;
437 }
438
439 return 0;
440}
441
442static int ioctl_set_param(struct drm_device *dev, void *data,
443 struct drm_file *file_priv)
444{
445 struct drm_omap_param *args = data;
446
447 switch (args->param) {
448 default:
449 DBG("unknown parameter %lld", args->param);
450 return -EINVAL;
451 }
452
453 return 0;
454}
455
Laurent Pinchartef3f4e92015-12-14 22:39:36 +0200456#define OMAP_BO_USER_MASK 0x00ffffff /* flags settable by userspace */
457
Rob Clarkcd5351f2011-11-12 12:09:40 -0600458static int ioctl_gem_new(struct drm_device *dev, void *data,
459 struct drm_file *file_priv)
460{
461 struct drm_omap_gem_new *args = data;
Laurent Pinchartef3f4e92015-12-14 22:39:36 +0200462 u32 flags = args->flags & OMAP_BO_USER_MASK;
463
Rob Clarkf5f94542012-12-04 13:59:12 -0600464 VERB("%p:%p: size=0x%08x, flags=%08x", dev, file_priv,
Laurent Pinchartef3f4e92015-12-14 22:39:36 +0200465 args->size.bytes, flags);
466
467 return omap_gem_new_handle(dev, file_priv, args->size, flags,
468 &args->handle);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600469}
470
Rob Clarkcd5351f2011-11-12 12:09:40 -0600471static int ioctl_gem_info(struct drm_device *dev, void *data,
472 struct drm_file *file_priv)
473{
474 struct drm_omap_gem_info *args = data;
475 struct drm_gem_object *obj;
476 int ret = 0;
477
Rob Clarkf5f94542012-12-04 13:59:12 -0600478 VERB("%p:%p: handle=%d", dev, file_priv, args->handle);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600479
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100480 obj = drm_gem_object_lookup(file_priv, args->handle);
YAMANE Toshiakic7f904b2012-11-14 19:30:38 +0900481 if (!obj)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600482 return -ENOENT;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600483
Rob Clarkf7f9f452011-12-05 19:19:22 -0600484 args->size = omap_gem_mmap_size(obj);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600485 args->offset = omap_gem_mmap_offset(obj);
486
Thomas Zimmermanne64d0222018-06-18 15:07:26 +0200487 drm_gem_object_put_unlocked(obj);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600488
489 return ret;
490}
491
Rob Clarkbaa70942013-08-02 13:27:49 -0400492static const struct drm_ioctl_desc ioctls[DRM_COMMAND_END - DRM_COMMAND_BASE] = {
Hemant Hariyani5f6ab8c2016-06-07 13:23:19 -0500493 DRM_IOCTL_DEF_DRV(OMAP_GET_PARAM, ioctl_get_param,
494 DRM_AUTH | DRM_RENDER_ALLOW),
495 DRM_IOCTL_DEF_DRV(OMAP_SET_PARAM, ioctl_set_param,
496 DRM_AUTH | DRM_MASTER | DRM_ROOT_ONLY),
497 DRM_IOCTL_DEF_DRV(OMAP_GEM_NEW, ioctl_gem_new,
498 DRM_AUTH | DRM_RENDER_ALLOW),
Laurent Pinchartd6f544f2017-05-09 01:27:11 +0300499 /* Deprecated, to be removed. */
500 DRM_IOCTL_DEF_DRV(OMAP_GEM_CPU_PREP, drm_noop,
Hemant Hariyani5f6ab8c2016-06-07 13:23:19 -0500501 DRM_AUTH | DRM_RENDER_ALLOW),
Laurent Pinchartd6f544f2017-05-09 01:27:11 +0300502 /* Deprecated, to be removed. */
503 DRM_IOCTL_DEF_DRV(OMAP_GEM_CPU_FINI, drm_noop,
Hemant Hariyani5f6ab8c2016-06-07 13:23:19 -0500504 DRM_AUTH | DRM_RENDER_ALLOW),
505 DRM_IOCTL_DEF_DRV(OMAP_GEM_INFO, ioctl_gem_info,
506 DRM_AUTH | DRM_RENDER_ALLOW),
Rob Clarkcd5351f2011-11-12 12:09:40 -0600507};
508
509/*
510 * drm driver funcs
511 */
512
Rob Clarkcd5351f2011-11-12 12:09:40 -0600513static int dev_open(struct drm_device *dev, struct drm_file *file)
514{
515 file->driver_priv = NULL;
516
517 DBG("open: dev=%p, file=%p", dev, file);
518
519 return 0;
520}
521
Laurent Pinchart78b68552012-05-17 13:27:22 +0200522static const struct vm_operations_struct omap_gem_vm_ops = {
Rob Clarkcd5351f2011-11-12 12:09:40 -0600523 .fault = omap_gem_fault,
524 .open = drm_gem_vm_open,
525 .close = drm_gem_vm_close,
526};
527
Rob Clarkff4f3872012-01-16 12:51:14 -0600528static const struct file_operations omapdriver_fops = {
Laurent Pinchart222025e2015-01-11 00:02:07 +0200529 .owner = THIS_MODULE,
530 .open = drm_open,
531 .unlocked_ioctl = drm_ioctl,
Tomi Valkeinen9d24159a2017-02-24 13:24:50 +0200532 .compat_ioctl = drm_compat_ioctl,
Laurent Pinchart222025e2015-01-11 00:02:07 +0200533 .release = drm_release,
534 .mmap = omap_gem_mmap,
535 .poll = drm_poll,
536 .read = drm_read,
537 .llseek = noop_llseek,
Rob Clarkff4f3872012-01-16 12:51:14 -0600538};
539
Rob Clarkcd5351f2011-11-12 12:09:40 -0600540static struct drm_driver omap_drm_driver = {
Tomi Valkeinen728fea72015-10-02 11:10:41 +0300541 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME |
Hemant Hariyani5f6ab8c2016-06-07 13:23:19 -0500542 DRIVER_ATOMIC | DRIVER_RENDER,
Laurent Pinchart222025e2015-01-11 00:02:07 +0200543 .open = dev_open,
Noralf Trønnesef62d302017-12-05 19:25:01 +0100544 .lastclose = drm_fb_helper_lastclose,
Andy Gross6169a1482011-12-15 21:05:17 -0600545#ifdef CONFIG_DEBUG_FS
Laurent Pinchart222025e2015-01-11 00:02:07 +0200546 .debugfs_init = omap_debugfs_init,
Andy Gross6169a1482011-12-15 21:05:17 -0600547#endif
Laurent Pinchart222025e2015-01-11 00:02:07 +0200548 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
549 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
550 .gem_prime_export = omap_gem_prime_export,
551 .gem_prime_import = omap_gem_prime_import,
Daniel Vetterf8466182018-05-25 19:39:25 +0300552 .gem_free_object_unlocked = omap_gem_free_object,
Laurent Pinchart222025e2015-01-11 00:02:07 +0200553 .gem_vm_ops = &omap_gem_vm_ops,
554 .dumb_create = omap_gem_dumb_create,
555 .dumb_map_offset = omap_gem_dumb_map_offset,
Laurent Pinchart222025e2015-01-11 00:02:07 +0200556 .ioctls = ioctls,
557 .num_ioctls = DRM_OMAP_NUM_IOCTLS,
558 .fops = &omapdriver_fops,
559 .name = DRIVER_NAME,
560 .desc = DRIVER_DESC,
561 .date = DRIVER_DATE,
562 .major = DRIVER_MAJOR,
563 .minor = DRIVER_MINOR,
564 .patchlevel = DRIVER_PATCHLEVEL,
Rob Clarkcd5351f2011-11-12 12:09:40 -0600565};
566
Laurent Pinchart6e471fa2017-05-06 02:57:12 +0300567static const struct soc_device_attribute omapdrm_soc_devices[] = {
568 { .family = "OMAP3", .data = (void *)0x3430 },
569 { .family = "OMAP4", .data = (void *)0x4430 },
570 { .family = "OMAP5", .data = (void *)0x5430 },
571 { .family = "DRA7", .data = (void *)0x0752 },
572 { /* sentinel */ }
573};
574
Laurent Pincharta82f03472018-02-13 14:00:19 +0200575static int omapdrm_init(struct omap_drm_private *priv, struct device *dev)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600576{
Laurent Pinchart6e471fa2017-05-06 02:57:12 +0300577 const struct soc_device_attribute *soc;
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200578 struct drm_device *ddev;
579 unsigned int i;
580 int ret;
581
Laurent Pincharta82f03472018-02-13 14:00:19 +0200582 DBG("%s", dev_name(dev));
Archit Taneja3a01ab22014-01-02 14:49:51 +0530583
Peter Ujfalusifb96b672018-02-12 11:44:36 +0200584 /* Allocate and initialize the DRM device. */
585 ddev = drm_dev_alloc(&omap_drm_driver, dev);
586 if (IS_ERR(ddev))
587 return PTR_ERR(ddev);
588
589 priv->ddev = ddev;
590 ddev->dev_private = priv;
591
Laurent Pincharta82f03472018-02-13 14:00:19 +0200592 priv->dev = dev;
Laurent Pinchartd3541ca2018-02-13 14:00:41 +0200593 priv->dss = omapdss_get_dss();
Laurent Pinchart50638ae2018-02-13 14:00:42 +0200594 priv->dispc = dispc_get_dispc(priv->dss);
Laurent Pinchartd3541ca2018-02-13 14:00:41 +0200595 priv->dispc_ops = dispc_get_ops(priv->dss);
Laurent Pinchart510c74c2017-08-11 16:49:08 +0300596
Laurent Pinchart64cb8172018-02-13 14:00:39 +0200597 omap_crtc_pre_init(priv);
Archit Taneja3a01ab22014-01-02 14:49:51 +0530598
Laurent Pinchart6e471fa2017-05-06 02:57:12 +0300599 soc = soc_device_match(omapdrm_soc_devices);
600 priv->omaprev = soc ? (unsigned int)soc->data : 0;
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200601 priv->wq = alloc_ordered_workqueue("omapdrm", 0);
602
Daniel Vetter5117bd82018-05-25 19:39:24 +0300603 mutex_init(&priv->list_lock);
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200604 INIT_LIST_HEAD(&priv->obj_list);
605
Peter Ujfalusia7631c42017-11-30 14:12:37 +0200606 /* Get memory bandwidth limits */
607 if (priv->dispc_ops->get_memory_bandwidth_limit)
608 priv->max_bandwidth =
Laurent Pinchart50638ae2018-02-13 14:00:42 +0200609 priv->dispc_ops->get_memory_bandwidth_limit(priv->dispc);
Peter Ujfalusia7631c42017-11-30 14:12:37 +0200610
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200611 omap_gem_init(ddev);
612
613 ret = omap_modeset_init(ddev);
614 if (ret) {
Laurent Pincharta82f03472018-02-13 14:00:19 +0200615 dev_err(priv->dev, "omap_modeset_init failed: ret=%d\n", ret);
Peter Ujfalusifb96b672018-02-12 11:44:36 +0200616 goto err_gem_deinit;
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200617 }
618
619 /* Initialize vblank handling, start with all CRTCs disabled. */
Laurent Pinchart2ee76792018-03-05 15:02:22 +0200620 ret = drm_vblank_init(ddev, priv->num_pipes);
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200621 if (ret) {
Laurent Pincharta82f03472018-02-13 14:00:19 +0200622 dev_err(priv->dev, "could not init vblank\n");
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200623 goto err_cleanup_modeset;
624 }
625
Laurent Pinchart2ee76792018-03-05 15:02:22 +0200626 for (i = 0; i < priv->num_pipes; i++)
627 drm_crtc_vblank_off(priv->pipes[i].crtc);
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200628
Tomi Valkeinenefd1f062018-02-09 09:36:23 +0200629 omap_fbdev_init(ddev);
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200630
631 drm_kms_helper_poll_init(ddev);
Peter Ujfalusi52b9ef22018-02-12 11:44:37 +0200632 omap_modeset_enable_external_hpd(ddev);
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200633
634 /*
635 * Register the DRM device with the core and the connectors with
636 * sysfs.
637 */
638 ret = drm_dev_register(ddev, 0);
639 if (ret)
640 goto err_cleanup_helpers;
641
642 return 0;
643
644err_cleanup_helpers:
Peter Ujfalusi52b9ef22018-02-12 11:44:37 +0200645 omap_modeset_disable_external_hpd(ddev);
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200646 drm_kms_helper_poll_fini(ddev);
Tomi Valkeinenefd1f062018-02-09 09:36:23 +0200647
648 omap_fbdev_fini(ddev);
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200649err_cleanup_modeset:
650 drm_mode_config_cleanup(ddev);
651 omap_drm_irq_uninstall(ddev);
Peter Ujfalusifb96b672018-02-12 11:44:36 +0200652err_gem_deinit:
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200653 omap_gem_deinit(ddev);
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200654 destroy_workqueue(priv->wq);
Laurent Pinchart2ee76792018-03-05 15:02:22 +0200655 omap_disconnect_pipelines(ddev);
Laurent Pinchart845417b2018-03-02 03:05:10 +0200656 omap_crtc_pre_uninit(priv);
Thomas Zimmermann08bafff2018-06-18 15:07:27 +0200657 drm_dev_put(ddev);
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200658 return ret;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600659}
660
Laurent Pincharta82f03472018-02-13 14:00:19 +0200661static void omapdrm_cleanup(struct omap_drm_private *priv)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600662{
Laurent Pincharta82f03472018-02-13 14:00:19 +0200663 struct drm_device *ddev = priv->ddev;
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200664
Rob Clarkcd5351f2011-11-12 12:09:40 -0600665 DBG("");
Andy Gross5c137792012-03-05 10:48:39 -0600666
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200667 drm_dev_unregister(ddev);
668
Peter Ujfalusi52b9ef22018-02-12 11:44:37 +0200669 omap_modeset_disable_external_hpd(ddev);
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200670 drm_kms_helper_poll_fini(ddev);
671
Tomi Valkeinenefd1f062018-02-09 09:36:23 +0200672 omap_fbdev_fini(ddev);
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200673
Tomi Valkeinen8a54aa92017-03-27 10:02:22 +0300674 drm_atomic_helper_shutdown(ddev);
675
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200676 drm_mode_config_cleanup(ddev);
677
678 omap_drm_irq_uninstall(ddev);
679 omap_gem_deinit(ddev);
680
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200681 destroy_workqueue(priv->wq);
Tomi Valkeinen707cf582014-04-02 13:47:43 +0300682
Laurent Pinchart2ee76792018-03-05 15:02:22 +0200683 omap_disconnect_pipelines(ddev);
Laurent Pinchart845417b2018-03-02 03:05:10 +0200684 omap_crtc_pre_uninit(priv);
Peter Ujfalusifb96b672018-02-12 11:44:36 +0200685
Thomas Zimmermann08bafff2018-06-18 15:07:27 +0200686 drm_dev_put(ddev);
Laurent Pincharta82f03472018-02-13 14:00:19 +0200687}
688
689static int pdev_probe(struct platform_device *pdev)
690{
691 struct omap_drm_private *priv;
692 int ret;
693
694 if (omapdss_is_initialized() == false)
695 return -EPROBE_DEFER;
696
697 ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
698 if (ret) {
699 dev_err(&pdev->dev, "Failed to set the DMA mask\n");
700 return ret;
701 }
702
703 /* Allocate and initialize the driver private structure. */
704 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
705 if (!priv)
706 return -ENOMEM;
707
708 platform_set_drvdata(pdev, priv);
709
710 ret = omapdrm_init(priv, &pdev->dev);
711 if (ret < 0)
712 kfree(priv);
713
714 return ret;
715}
716
717static int pdev_remove(struct platform_device *pdev)
718{
719 struct omap_drm_private *priv = platform_get_drvdata(pdev);
720
721 omapdrm_cleanup(priv);
722 kfree(priv);
Daniel Vetterfd3c0252013-12-11 11:34:26 +0100723
Rob Clarkcd5351f2011-11-12 12:09:40 -0600724 return 0;
725}
726
Grygorii Strashko8450c8d2015-02-26 15:57:17 +0200727#ifdef CONFIG_PM_SLEEP
Tomi Valkeinenccd7b5e2014-11-14 15:18:28 +0200728static int omap_drm_suspend(struct device *dev)
729{
Laurent Pincharta82f03472018-02-13 14:00:19 +0200730 struct omap_drm_private *priv = dev_get_drvdata(dev);
731 struct drm_device *drm_dev = priv->ddev;
Tomi Valkeinenccd7b5e2014-11-14 15:18:28 +0200732
Laurent Pinchartd2c53162018-09-04 17:08:33 +0300733 return drm_mode_config_helper_suspend(drm_dev);
Tomi Valkeinenccd7b5e2014-11-14 15:18:28 +0200734}
735
736static int omap_drm_resume(struct device *dev)
737{
Laurent Pincharta82f03472018-02-13 14:00:19 +0200738 struct omap_drm_private *priv = dev_get_drvdata(dev);
739 struct drm_device *drm_dev = priv->ddev;
Tomi Valkeinenccd7b5e2014-11-14 15:18:28 +0200740
Laurent Pinchartd2c53162018-09-04 17:08:33 +0300741 drm_mode_config_helper_resume(drm_dev);
Tomi Valkeinenccd7b5e2014-11-14 15:18:28 +0200742
Laurent Pinchart7fb15c42017-10-13 17:58:58 +0300743 return omap_gem_resume(drm_dev);
Tomi Valkeinenccd7b5e2014-11-14 15:18:28 +0200744}
Andy Grosse78edba2012-12-19 14:53:37 -0600745#endif
746
Grygorii Strashko8450c8d2015-02-26 15:57:17 +0200747static SIMPLE_DEV_PM_OPS(omapdrm_pm_ops, omap_drm_suspend, omap_drm_resume);
748
Tomi Valkeinen6717cd22013-04-10 10:44:00 +0300749static struct platform_driver pdev = {
Laurent Pinchart222025e2015-01-11 00:02:07 +0200750 .driver = {
Tomi Valkeinenf64eafa2017-08-16 12:43:55 +0300751 .name = "omapdrm",
Laurent Pinchart222025e2015-01-11 00:02:07 +0200752 .pm = &omapdrm_pm_ops,
Laurent Pinchart222025e2015-01-11 00:02:07 +0200753 },
754 .probe = pdev_probe,
755 .remove = pdev_remove,
Rob Clarkcd5351f2011-11-12 12:09:40 -0600756};
757
Thierry Redinge1c49bd2015-12-02 17:23:31 +0100758static struct platform_driver * const drivers[] = {
759 &omap_dmm_driver,
760 &pdev,
761};
762
Rob Clarkcd5351f2011-11-12 12:09:40 -0600763static int __init omap_drm_init(void)
764{
765 DBG("init");
Tomi Valkeinenea7e3a62014-04-02 14:31:50 +0300766
Thierry Redinge1c49bd2015-12-02 17:23:31 +0100767 return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
Rob Clarkcd5351f2011-11-12 12:09:40 -0600768}
769
770static void __exit omap_drm_fini(void)
771{
772 DBG("fini");
Tomi Valkeinenea7e3a62014-04-02 14:31:50 +0300773
Thierry Redinge1c49bd2015-12-02 17:23:31 +0100774 platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
Rob Clarkcd5351f2011-11-12 12:09:40 -0600775}
776
777/* need late_initcall() so we load after dss_driver's are loaded */
778late_initcall(omap_drm_init);
779module_exit(omap_drm_fini);
780
781MODULE_AUTHOR("Rob Clark <rob@ti.com>");
782MODULE_DESCRIPTION("OMAP DRM Display Driver");
783MODULE_ALIAS("platform:" DRIVER_NAME);
784MODULE_LICENSE("GPL v2");