blob: 2720a58ccd90a3f7da5fcbe76dcfeac60171db3d [file] [log] [blame]
Thomas Gleixnercaab2772019-06-03 07:44:50 +02001// SPDX-License-Identifier: GPL-2.0-only
Rob Clarkcd5351f2011-11-12 12:09:40 -06002/*
Alexander A. Klimov1b409fd2020-07-13 14:28:59 +02003 * Copyright (C) 2011 Texas Instruments Incorporated - https://www.ti.com/
Rob Clarkcd5351f2011-11-12 12:09:40 -06004 * Author: Rob Clark <rob@ti.com>
Rob Clarkcd5351f2011-11-12 12:09:40 -06005 */
6
Sam Ravnborg81f61562019-07-16 08:42:10 +02007#include <linux/dma-mapping.h>
8#include <linux/platform_device.h>
Peter Ujfalusieb5bc1f2018-02-12 11:44:39 +02009#include <linux/sort.h>
Laurent Pinchart6e471fa2017-05-06 02:57:12 +030010#include <linux/sys_soc.h>
11
Laurent Pinchart748471a52015-03-05 23:42:39 +020012#include <drm/drm_atomic.h>
Laurent Pinchartcef77d42015-03-05 21:50:00 +020013#include <drm/drm_atomic_helper.h>
Boris Brezillonee68c742019-08-26 17:26:29 +020014#include <drm/drm_bridge.h>
Laurent Pinchartf40f4e42020-02-26 13:24:58 +020015#include <drm/drm_bridge_connector.h>
Sam Ravnborg81f61562019-07-16 08:42:10 +020016#include <drm/drm_drv.h>
Laurent Pinchart2d278f52015-03-05 21:31:37 +020017#include <drm/drm_fb_helper.h>
Sam Ravnborg81f61562019-07-16 08:42:10 +020018#include <drm/drm_file.h>
19#include <drm/drm_ioctl.h>
Laurent Pinchartf40f4e42020-02-26 13:24:58 +020020#include <drm/drm_panel.h>
Sam Ravnborg81f61562019-07-16 08:42:10 +020021#include <drm/drm_prime.h>
22#include <drm/drm_probe_helper.h>
23#include <drm/drm_vblank.h>
Rob Clarkcd5351f2011-11-12 12:09:40 -060024
Andy Gross5c137792012-03-05 10:48:39 -060025#include "omap_dmm_tiler.h"
Laurent Pinchart2d278f52015-03-05 21:31:37 +020026#include "omap_drv.h"
Rob Clarkcd5351f2011-11-12 12:09:40 -060027
28#define DRIVER_NAME MODULE_NAME
29#define DRIVER_DESC "OMAP DRM"
30#define DRIVER_DATE "20110917"
31#define DRIVER_MAJOR 1
32#define DRIVER_MINOR 0
33#define DRIVER_PATCHLEVEL 0
34
Rob Clarkcd5351f2011-11-12 12:09:40 -060035/*
36 * mode config funcs
37 */
38
39/* Notes about mapping DSS and DRM entities:
40 * CRTC: overlay
41 * encoder: manager.. with some extension to allow one primary CRTC
42 * and zero or more video CRTC's to be mapped to one encoder?
43 * connector: dssdev.. manager can be attached/detached from different
44 * devices
45 */
46
Tomi Valkeinen5f741b32015-05-29 16:01:18 +030047static void omap_atomic_wait_for_completion(struct drm_device *dev,
48 struct drm_atomic_state *old_state)
49{
Maarten Lankhorst34d88232017-07-19 16:39:17 +020050 struct drm_crtc_state *new_crtc_state;
Tomi Valkeinen5f741b32015-05-29 16:01:18 +030051 struct drm_crtc *crtc;
52 unsigned int i;
53 int ret;
54
Maarten Lankhorst34d88232017-07-19 16:39:17 +020055 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
56 if (!new_crtc_state->active)
Tomi Valkeinen5f741b32015-05-29 16:01:18 +030057 continue;
58
59 ret = omap_crtc_wait_pending(crtc);
60
61 if (!ret)
62 dev_warn(dev->dev,
63 "atomic complete timeout (pipe %u)!\n", i);
64 }
65}
66
Laurent Pincharta9e6f9f2017-05-09 01:27:10 +030067static void omap_atomic_commit_tail(struct drm_atomic_state *old_state)
Laurent Pinchart748471a52015-03-05 23:42:39 +020068{
Laurent Pincharta9e6f9f2017-05-09 01:27:10 +030069 struct drm_device *dev = old_state->dev;
Laurent Pinchart748471a52015-03-05 23:42:39 +020070 struct omap_drm_private *priv = dev->dev_private;
Daniel Vetter250aa222021-01-21 16:29:56 +010071 bool fence_cookie = dma_fence_begin_signalling();
Laurent Pinchart748471a52015-03-05 23:42:39 +020072
Tomi Valkeinendac62bc2020-12-15 12:46:26 +020073 dispc_runtime_get(priv->dispc);
Laurent Pinchart69fb7c82015-05-28 02:09:56 +030074
Laurent Pincharta9e6f9f2017-05-09 01:27:10 +030075 /* Apply the atomic update. */
Laurent Pinchart748471a52015-03-05 23:42:39 +020076 drm_atomic_helper_commit_modeset_disables(dev, old_state);
Jyri Sarha897145d2017-01-27 12:04:55 +020077
Tomi Valkeinenfc5cc9672017-08-23 12:19:02 +030078 if (priv->omaprev != 0x3430) {
79 /* With the current dss dispc implementation we have to enable
80 * the new modeset before we can commit planes. The dispc ovl
81 * configuration relies on the video mode configuration been
82 * written into the HW when the ovl configuration is
83 * calculated.
84 *
85 * This approach is not ideal because after a mode change the
86 * plane update is executed only after the first vblank
87 * interrupt. The dispc implementation should be fixed so that
88 * it is able use uncommitted drm state information.
89 */
90 drm_atomic_helper_commit_modeset_enables(dev, old_state);
91 omap_atomic_wait_for_completion(dev, old_state);
Jyri Sarha897145d2017-01-27 12:04:55 +020092
Tomi Valkeinenfc5cc9672017-08-23 12:19:02 +030093 drm_atomic_helper_commit_planes(dev, old_state, 0);
Tomi Valkeinenfc5cc9672017-08-23 12:19:02 +030094 } else {
95 /*
96 * OMAP3 DSS seems to have issues with the work-around above,
97 * resulting in endless sync losts if a crtc is enabled without
98 * a plane. For now, skip the WA for OMAP3.
99 */
100 drm_atomic_helper_commit_planes(dev, old_state, 0);
101
102 drm_atomic_helper_commit_modeset_enables(dev, old_state);
Tomi Valkeinenfc5cc9672017-08-23 12:19:02 +0300103 }
Laurent Pincharta9e6f9f2017-05-09 01:27:10 +0300104
Daniel Vetter250aa222021-01-21 16:29:56 +0100105 drm_atomic_helper_commit_hw_done(old_state);
106
107 dma_fence_end_signalling(fence_cookie);
108
Laurent Pincharta9e6f9f2017-05-09 01:27:10 +0300109 /*
110 * Wait for completion of the page flips to ensure that old buffers
111 * can't be touched by the hardware anymore before cleaning up planes.
112 */
Tomi Valkeinen5f741b32015-05-29 16:01:18 +0300113 omap_atomic_wait_for_completion(dev, old_state);
Laurent Pinchart748471a52015-03-05 23:42:39 +0200114
115 drm_atomic_helper_cleanup_planes(dev, old_state);
116
Tomi Valkeinendac62bc2020-12-15 12:46:26 +0200117 dispc_runtime_put(priv->dispc);
Laurent Pinchart748471a52015-03-05 23:42:39 +0200118}
119
Benoit Parrote02b5cc2021-11-17 15:19:28 +0100120static int drm_atomic_state_normalized_zpos_cmp(const void *a, const void *b)
121{
122 const struct drm_plane_state *sa = *(struct drm_plane_state **)a;
123 const struct drm_plane_state *sb = *(struct drm_plane_state **)b;
124
125 if (sa->normalized_zpos != sb->normalized_zpos)
126 return sa->normalized_zpos - sb->normalized_zpos;
127 else
128 return sa->plane->base.id - sb->plane->base.id;
129}
130
131/*
132 * This replaces the drm_atomic_normalize_zpos to handle the dual overlay case.
133 *
134 * Since both halves need to be 'appear' side by side the zpos is
135 * recalculated when dealing with dual overlay cases so that the other
136 * planes zpos is consistent.
137 */
138static int omap_atomic_update_normalize_zpos(struct drm_device *dev,
139 struct drm_atomic_state *state)
140{
141 struct drm_crtc *crtc;
142 struct drm_crtc_state *old_state, *new_state;
143 struct drm_plane *plane;
144 int c, i, n, inc;
145 int total_planes = dev->mode_config.num_total_plane;
146 struct drm_plane_state **states;
147 int ret = 0;
148
149 states = kmalloc_array(total_planes, sizeof(*states), GFP_KERNEL);
150 if (!states)
151 return -ENOMEM;
152
153 for_each_oldnew_crtc_in_state(state, crtc, old_state, new_state, c) {
154 if (old_state->plane_mask == new_state->plane_mask &&
155 !new_state->zpos_changed)
156 continue;
157
158 /* Reset plane increment and index value for every crtc */
159 n = 0;
160
161 /*
162 * Normalization process might create new states for planes
163 * which normalized_zpos has to be recalculated.
164 */
165 drm_for_each_plane_mask(plane, dev, new_state->plane_mask) {
166 struct drm_plane_state *plane_state =
167 drm_atomic_get_plane_state(new_state->state,
168 plane);
169 if (IS_ERR(plane_state)) {
170 ret = PTR_ERR(plane_state);
171 goto done;
172 }
173 states[n++] = plane_state;
174 }
175
176 sort(states, n, sizeof(*states),
177 drm_atomic_state_normalized_zpos_cmp, NULL);
178
179 for (i = 0, inc = 0; i < n; i++) {
180 plane = states[i]->plane;
181
182 states[i]->normalized_zpos = i + inc;
183 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] updated normalized zpos value %d\n",
184 plane->base.id, plane->name,
185 states[i]->normalized_zpos);
186
187 if (is_omap_plane_dual_overlay(states[i]))
188 inc++;
189 }
190 new_state->zpos_changed = true;
191 }
192
193done:
194 kfree(states);
195 return ret;
196}
197
198static int omap_atomic_check(struct drm_device *dev,
199 struct drm_atomic_state *state)
200{
201 int ret;
202
203 ret = drm_atomic_helper_check(dev, state);
204 if (ret)
205 return ret;
206
207 if (dev->mode_config.normalize_zpos) {
208 ret = omap_atomic_update_normalize_zpos(dev, state);
209 if (ret)
210 return ret;
211 }
212
213 return 0;
214}
215
Laurent Pincharta9e6f9f2017-05-09 01:27:10 +0300216static const struct drm_mode_config_helper_funcs omap_mode_config_helper_funcs = {
217 .atomic_commit_tail = omap_atomic_commit_tail,
218};
Laurent Pinchart748471a52015-03-05 23:42:39 +0200219
Laurent Pincharte6ecefa2012-05-17 13:27:23 +0200220static const struct drm_mode_config_funcs omap_mode_config_funcs = {
Rob Clarkcd5351f2011-11-12 12:09:40 -0600221 .fb_create = omap_framebuffer_create,
Noralf Trønnesef62d302017-12-05 19:25:01 +0100222 .output_poll_changed = drm_fb_helper_output_poll_changed,
Benoit Parrote02b5cc2021-11-17 15:19:28 +0100223 .atomic_check = omap_atomic_check,
Laurent Pincharta9e6f9f2017-05-09 01:27:10 +0300224 .atomic_commit = drm_atomic_helper_commit,
Rob Clarkcd5351f2011-11-12 12:09:40 -0600225};
226
Benoit Parrot6e422012021-11-17 15:19:25 +0100227/* Global/shared object state funcs */
228
229/*
230 * This is a helper that returns the private state currently in operation.
231 * Note that this would return the "old_state" if called in the atomic check
232 * path, and the "new_state" after the atomic swap has been done.
233 */
234struct omap_global_state *
235omap_get_existing_global_state(struct omap_drm_private *priv)
236{
237 return to_omap_global_state(priv->glob_obj.state);
238}
239
240/*
241 * This acquires the modeset lock set aside for global state, creates
242 * a new duplicated private object state.
243 */
244struct omap_global_state *__must_check
245omap_get_global_state(struct drm_atomic_state *s)
246{
247 struct omap_drm_private *priv = s->dev->dev_private;
248 struct drm_private_state *priv_state;
249
250 priv_state = drm_atomic_get_private_obj_state(s, &priv->glob_obj);
251 if (IS_ERR(priv_state))
252 return ERR_CAST(priv_state);
253
254 return to_omap_global_state(priv_state);
255}
256
257static struct drm_private_state *
258omap_global_duplicate_state(struct drm_private_obj *obj)
259{
260 struct omap_global_state *state;
261
262 state = kmemdup(obj->state, sizeof(*state), GFP_KERNEL);
263 if (!state)
264 return NULL;
265
266 __drm_atomic_helper_private_obj_duplicate_state(obj, &state->base);
267
268 return &state->base;
269}
270
271static void omap_global_destroy_state(struct drm_private_obj *obj,
272 struct drm_private_state *state)
273{
274 struct omap_global_state *omap_state = to_omap_global_state(state);
275
276 kfree(omap_state);
277}
278
279static const struct drm_private_state_funcs omap_global_state_funcs = {
280 .atomic_duplicate_state = omap_global_duplicate_state,
281 .atomic_destroy_state = omap_global_destroy_state,
282};
283
284static int omap_global_obj_init(struct drm_device *dev)
285{
286 struct omap_drm_private *priv = dev->dev_private;
287 struct omap_global_state *state;
288
289 state = kzalloc(sizeof(*state), GFP_KERNEL);
290 if (!state)
291 return -ENOMEM;
292
293 drm_atomic_private_obj_init(dev, &priv->glob_obj, &state->base,
294 &omap_global_state_funcs);
295 return 0;
296}
297
298static void omap_global_obj_fini(struct omap_drm_private *priv)
299{
300 drm_atomic_private_obj_fini(&priv->glob_obj);
301}
302
Laurent Pinchart2ee76792018-03-05 15:02:22 +0200303static void omap_disconnect_pipelines(struct drm_device *ddev)
Archit Tanejacc823bd2014-01-02 14:49:52 +0530304{
Peter Ujfalusi52b9ef22018-02-12 11:44:37 +0200305 struct omap_drm_private *priv = ddev->dev_private;
306 unsigned int i;
Archit Tanejacc823bd2014-01-02 14:49:52 +0530307
Laurent Pinchart2ee76792018-03-05 15:02:22 +0200308 for (i = 0; i < priv->num_pipes; i++) {
Laurent Pinchart511afb42018-03-04 23:42:36 +0200309 struct omap_drm_pipeline *pipe = &priv->pipes[i];
Peter Ujfalusi52b9ef22018-02-12 11:44:37 +0200310
Laurent Pinchart511afb42018-03-04 23:42:36 +0200311 omapdss_device_disconnect(NULL, pipe->output);
312
313 omapdss_device_put(pipe->output);
Laurent Pinchart511afb42018-03-04 23:42:36 +0200314 pipe->output = NULL;
Peter Ujfalusi52b9ef22018-02-12 11:44:37 +0200315 }
316
Laurent Pincharte48f9f12018-03-07 00:01:33 +0200317 memset(&priv->channels, 0, sizeof(priv->channels));
318
Laurent Pinchart2ee76792018-03-05 15:02:22 +0200319 priv->num_pipes = 0;
Archit Tanejacc823bd2014-01-02 14:49:52 +0530320}
Archit Taneja0d8f3712013-03-26 19:15:19 +0530321
Laurent Pinchart2ee76792018-03-05 15:02:22 +0200322static int omap_connect_pipelines(struct drm_device *ddev)
Archit Taneja3a01ab22014-01-02 14:49:51 +0530323{
Peter Ujfalusi52b9ef22018-02-12 11:44:37 +0200324 struct omap_drm_private *priv = ddev->dev_private;
Laurent Pinchart511afb42018-03-04 23:42:36 +0200325 struct omap_dss_device *output = NULL;
Peter Ujfalusi52b9ef22018-02-12 11:44:37 +0200326 int r;
Peter Ujfalusia09d2bc2016-05-03 22:08:01 +0300327
Laurent Pinchart511afb42018-03-04 23:42:36 +0200328 for_each_dss_output(output) {
329 r = omapdss_device_connect(priv->dss, NULL, output);
Archit Taneja3a01ab22014-01-02 14:49:51 +0530330 if (r == -EPROBE_DEFER) {
Laurent Pinchart511afb42018-03-04 23:42:36 +0200331 omapdss_device_put(output);
Laurent Pincharta4e26522018-09-23 14:13:15 +0300332 return r;
Archit Taneja3a01ab22014-01-02 14:49:51 +0530333 } else if (r) {
Laurent Pinchart511afb42018-03-04 23:42:36 +0200334 dev_warn(output->dev, "could not connect output %s\n",
335 output->name);
Peter Ujfalusi52b9ef22018-02-12 11:44:37 +0200336 } else {
Laurent Pinchart511afb42018-03-04 23:42:36 +0200337 struct omap_drm_pipeline *pipe;
338
339 pipe = &priv->pipes[priv->num_pipes++];
340 pipe->output = omapdss_device_get(output);
Laurent Pinchart511afb42018-03-04 23:42:36 +0200341
Laurent Pinchart2ee76792018-03-05 15:02:22 +0200342 if (priv->num_pipes == ARRAY_SIZE(priv->pipes)) {
Laurent Pinchart511afb42018-03-04 23:42:36 +0200343 /* To balance the 'for_each_dss_output' loop */
344 omapdss_device_put(output);
Peter Ujfalusi52b9ef22018-02-12 11:44:37 +0200345 break;
346 }
Archit Taneja3a01ab22014-01-02 14:49:51 +0530347 }
348 }
349
Archit Taneja3a01ab22014-01-02 14:49:51 +0530350 return 0;
Laurent Pincharta4e26522018-09-23 14:13:15 +0300351}
Archit Taneja3a01ab22014-01-02 14:49:51 +0530352
Laurent Pincharta4e26522018-09-23 14:13:15 +0300353static int omap_compare_pipelines(const void *a, const void *b)
354{
355 const struct omap_drm_pipeline *pipe1 = a;
356 const struct omap_drm_pipeline *pipe2 = b;
Archit Taneja3a01ab22014-01-02 14:49:51 +0530357
Laurent Pincharta4e26522018-09-23 14:13:15 +0300358 if (pipe1->alias_id > pipe2->alias_id)
359 return 1;
360 else if (pipe1->alias_id < pipe2->alias_id)
361 return -1;
362 return 0;
Archit Taneja3a01ab22014-01-02 14:49:51 +0530363}
Rob Clarkcd5351f2011-11-12 12:09:40 -0600364
Laurent Pincharte2cd09b2015-03-06 17:16:43 +0200365static int omap_modeset_init_properties(struct drm_device *dev)
366{
367 struct omap_drm_private *priv = dev->dev_private;
Tomi Valkeinendac62bc2020-12-15 12:46:26 +0200368 unsigned int num_planes = dispc_get_num_ovls(priv->dispc);
Laurent Pincharte2cd09b2015-03-06 17:16:43 +0200369
Laurent Pinchartdff6c242017-05-09 01:27:14 +0300370 priv->zorder_prop = drm_property_create_range(dev, 0, "zorder", 0,
371 num_planes - 1);
Laurent Pincharte2cd09b2015-03-06 17:16:43 +0200372 if (!priv->zorder_prop)
373 return -ENOMEM;
374
375 return 0;
376}
377
Laurent Pinchart79107f22018-09-23 12:58:15 +0300378static int omap_display_id(struct omap_dss_device *output)
379{
380 struct device_node *node = NULL;
381
Sebastian Reichel4a555512020-12-15 12:46:16 +0200382 if (output->bridge) {
Laurent Pinchart79107f22018-09-23 12:58:15 +0300383 struct drm_bridge *bridge = output->bridge;
384
Boris Brezillonfadf8722019-12-03 15:15:06 +0100385 while (drm_bridge_get_next_bridge(bridge))
386 bridge = drm_bridge_get_next_bridge(bridge);
Laurent Pinchart79107f22018-09-23 12:58:15 +0300387
388 node = bridge->of_node;
389 }
390
391 return node ? of_alias_get_id(node, "display") : -ENODEV;
392}
393
Rob Clarkcd5351f2011-11-12 12:09:40 -0600394static int omap_modeset_init(struct drm_device *dev)
395{
Rob Clarkcd5351f2011-11-12 12:09:40 -0600396 struct omap_drm_private *priv = dev->dev_private;
Tomi Valkeinendac62bc2020-12-15 12:46:26 +0200397 int num_ovls = dispc_get_num_ovls(priv->dispc);
398 int num_mgrs = dispc_get_num_mgrs(priv->dispc);
Peter Ujfalusi52b9ef22018-02-12 11:44:37 +0200399 unsigned int i;
Laurent Pinchartfb9a35f2015-01-11 16:30:44 +0200400 int ret;
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200401 u32 plane_crtc_mask;
Tomi Valkeinen04b1fc02013-05-14 10:55:19 +0300402
Laurent Pincharta4e26522018-09-23 14:13:15 +0300403 if (!omapdss_stack_is_ready())
404 return -EPROBE_DEFER;
405
Laurent Pincharte2cd09b2015-03-06 17:16:43 +0200406 ret = omap_modeset_init_properties(dev);
407 if (ret < 0)
408 return ret;
409
Rob Clarkf5f94542012-12-04 13:59:12 -0600410 /*
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200411 * This function creates exactly one connector, encoder, crtc,
412 * and primary plane per each connected dss-device. Each
413 * connector->encoder->crtc chain is expected to be separate
414 * and each crtc is connect to a single dss-channel. If the
415 * configuration does not match the expectations or exceeds
416 * the available resources, the configuration is rejected.
Rob Clarkf5f94542012-12-04 13:59:12 -0600417 */
Laurent Pincharta4e26522018-09-23 14:13:15 +0300418 ret = omap_connect_pipelines(dev);
419 if (ret < 0)
420 return ret;
421
Laurent Pinchart2ee76792018-03-05 15:02:22 +0200422 if (priv->num_pipes > num_mgrs || priv->num_pipes > num_ovls) {
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200423 dev_err(dev->dev, "%s(): Too many connected displays\n",
424 __func__);
425 return -EINVAL;
426 }
427
Laurent Pinchartac3b1312018-03-05 19:11:30 +0200428 /* Create all planes first. They can all be put to any CRTC. */
Laurent Pinchart2ee76792018-03-05 15:02:22 +0200429 plane_crtc_mask = (1 << priv->num_pipes) - 1;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600430
Laurent Pinchartac3b1312018-03-05 19:11:30 +0200431 for (i = 0; i < num_ovls; i++) {
Laurent Pinchart2ee76792018-03-05 15:02:22 +0200432 enum drm_plane_type type = i < priv->num_pipes
Laurent Pinchartac3b1312018-03-05 19:11:30 +0200433 ? DRM_PLANE_TYPE_PRIMARY
434 : DRM_PLANE_TYPE_OVERLAY;
435 struct drm_plane *plane;
436
437 if (WARN_ON(priv->num_planes >= ARRAY_SIZE(priv->planes)))
438 return -EINVAL;
439
440 plane = omap_plane_init(dev, i, type, plane_crtc_mask);
441 if (IS_ERR(plane))
442 return PTR_ERR(plane);
443
444 priv->planes[priv->num_planes++] = plane;
445 }
446
Laurent Pinchart79107f22018-09-23 12:58:15 +0300447 /*
448 * Create the encoders, attach the bridges and get the pipeline alias
449 * IDs.
450 */
Laurent Pinchart2ee76792018-03-05 15:02:22 +0200451 for (i = 0; i < priv->num_pipes; i++) {
452 struct omap_drm_pipeline *pipe = &priv->pipes[i];
Laurent Pincharta4e26522018-09-23 14:13:15 +0300453 int id;
454
455 pipe->encoder = omap_encoder_init(dev, pipe->output);
456 if (!pipe->encoder)
457 return -ENOMEM;
458
Laurent Pinchart79107f22018-09-23 12:58:15 +0300459 if (pipe->output->bridge) {
460 ret = drm_bridge_attach(pipe->encoder,
Laurent Pinchartf40f4e42020-02-26 13:24:58 +0200461 pipe->output->bridge, NULL,
462 DRM_BRIDGE_ATTACH_NO_CONNECTOR);
Laurent Pinchartfb8d6172021-03-23 23:50:08 +0200463 if (ret < 0)
Laurent Pinchart79107f22018-09-23 12:58:15 +0300464 return ret;
465 }
466
467 id = omap_display_id(pipe->output);
Laurent Pincharta4e26522018-09-23 14:13:15 +0300468 pipe->alias_id = id >= 0 ? id : i;
469 }
470
471 /* Sort the pipelines by DT aliases. */
472 sort(priv->pipes, priv->num_pipes, sizeof(priv->pipes[0]),
473 omap_compare_pipelines, NULL);
474
475 /*
476 * Populate the pipeline lookup table by DISPC channel. Only one display
477 * is allowed per channel.
478 */
479 for (i = 0; i < priv->num_pipes; ++i) {
480 struct omap_drm_pipeline *pipe = &priv->pipes[i];
481 enum omap_channel channel = pipe->output->dispc_channel;
482
483 if (WARN_ON(priv->channels[channel] != NULL))
484 return -EINVAL;
485
486 priv->channels[channel] = pipe;
487 }
488
489 /* Create the connectors and CRTCs. */
490 for (i = 0; i < priv->num_pipes; i++) {
491 struct omap_drm_pipeline *pipe = &priv->pipes[i];
492 struct drm_encoder *encoder = pipe->encoder;
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200493 struct drm_crtc *crtc;
Rob Clarkf5f94542012-12-04 13:59:12 -0600494
Sebastian Reichelfe5f6e52020-12-15 12:46:15 +0200495 pipe->connector = drm_bridge_connector_init(dev, encoder);
496 if (IS_ERR(pipe->connector)) {
497 dev_err(priv->dev,
498 "unable to create bridge connector for %s\n",
499 pipe->output->name);
500 return PTR_ERR(pipe->connector);
Laurent Pinchartf40f4e42020-02-26 13:24:58 +0200501 }
Rob Clarkf5f94542012-12-04 13:59:12 -0600502
Laurent Pinchartf40f4e42020-02-26 13:24:58 +0200503 drm_connector_attach_encoder(pipe->connector, encoder);
504
Laurent Pinchart00b30e72018-03-06 23:37:25 +0200505 crtc = omap_crtc_init(dev, pipe, priv->planes[i]);
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200506 if (IS_ERR(crtc))
507 return PTR_ERR(crtc);
508
Laurent Pinchartf9699362018-03-05 14:47:47 +0200509 encoder->possible_crtcs = 1 << i;
Laurent Pinchart2ee76792018-03-05 15:02:22 +0200510 pipe->crtc = crtc;
Archit Taneja0d8f3712013-03-26 19:15:19 +0530511 }
512
Laurent Pinchart2ee76792018-03-05 15:02:22 +0200513 DBG("registered %u planes, %u crtcs/encoders/connectors\n",
514 priv->num_planes, priv->num_pipes);
Archit Taneja0d8f3712013-03-26 19:15:19 +0530515
Tomi Valkeinen1e907112016-08-23 12:35:39 +0300516 dev->mode_config.min_width = 8;
517 dev->mode_config.min_height = 2;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600518
Tomi Valkeinen1915d7f2018-01-10 11:31:18 +0200519 /*
520 * Note: these values are used for multiple independent things:
521 * connector mode filtering, buffer sizes, crtc sizes...
522 * Use big enough values here to cover all use cases, and do more
523 * specific checking in the respective code paths.
Rob Clarkcd5351f2011-11-12 12:09:40 -0600524 */
Tomi Valkeinen1915d7f2018-01-10 11:31:18 +0200525 dev->mode_config.max_width = 8192;
526 dev->mode_config.max_height = 8192;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600527
Peter Ujfalusi23936ba2018-03-21 12:20:29 +0200528 /* We want the zpos to be normalized */
529 dev->mode_config.normalize_zpos = true;
530
Rob Clarkcd5351f2011-11-12 12:09:40 -0600531 dev->mode_config.funcs = &omap_mode_config_funcs;
Laurent Pincharta9e6f9f2017-05-09 01:27:10 +0300532 dev->mode_config.helper_private = &omap_mode_config_helper_funcs;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600533
Laurent Pinchart69a12262015-03-05 21:38:16 +0200534 drm_mode_config_reset(dev);
535
Laurent Pinchart728ae8d2015-05-28 00:21:29 +0300536 omap_drm_irq_install(dev);
537
Rob Clarkcd5351f2011-11-12 12:09:40 -0600538 return 0;
539}
540
Laurent Pinchartf40f4e42020-02-26 13:24:58 +0200541static void omap_modeset_fini(struct drm_device *ddev)
542{
Laurent Pinchartf40f4e42020-02-26 13:24:58 +0200543 omap_drm_irq_uninstall(ddev);
544
Laurent Pinchartf40f4e42020-02-26 13:24:58 +0200545 drm_mode_config_cleanup(ddev);
546}
547
Rob Clarkcd5351f2011-11-12 12:09:40 -0600548/*
Peter Ujfalusi3c596802017-06-02 15:26:35 +0300549 * Enable the HPD in external components if supported
550 */
Peter Ujfalusi52b9ef22018-02-12 11:44:37 +0200551static void omap_modeset_enable_external_hpd(struct drm_device *ddev)
Peter Ujfalusi3c596802017-06-02 15:26:35 +0300552{
Peter Ujfalusi52b9ef22018-02-12 11:44:37 +0200553 struct omap_drm_private *priv = ddev->dev_private;
Laurent Pinchart79107f22018-09-23 12:58:15 +0300554 unsigned int i;
Peter Ujfalusi3c596802017-06-02 15:26:35 +0300555
Laurent Pinchart79107f22018-09-23 12:58:15 +0300556 for (i = 0; i < priv->num_pipes; i++) {
Laurent Pinchartf40f4e42020-02-26 13:24:58 +0200557 struct drm_connector *connector = priv->pipes[i].connector;
558
559 if (!connector)
560 continue;
561
Laurent Pinchart75fb9682020-02-26 13:25:00 +0200562 if (priv->pipes[i].output->bridge)
Laurent Pinchartf40f4e42020-02-26 13:24:58 +0200563 drm_bridge_connector_enable_hpd(connector);
Laurent Pinchart79107f22018-09-23 12:58:15 +0300564 }
Peter Ujfalusi3c596802017-06-02 15:26:35 +0300565}
566
567/*
568 * Disable the HPD in external components if supported
569 */
Peter Ujfalusi52b9ef22018-02-12 11:44:37 +0200570static void omap_modeset_disable_external_hpd(struct drm_device *ddev)
Peter Ujfalusi3c596802017-06-02 15:26:35 +0300571{
Peter Ujfalusi52b9ef22018-02-12 11:44:37 +0200572 struct omap_drm_private *priv = ddev->dev_private;
Laurent Pinchart79107f22018-09-23 12:58:15 +0300573 unsigned int i;
Peter Ujfalusi3c596802017-06-02 15:26:35 +0300574
Laurent Pinchart79107f22018-09-23 12:58:15 +0300575 for (i = 0; i < priv->num_pipes; i++) {
Laurent Pinchartf40f4e42020-02-26 13:24:58 +0200576 struct drm_connector *connector = priv->pipes[i].connector;
577
578 if (!connector)
579 continue;
580
Laurent Pinchart75fb9682020-02-26 13:25:00 +0200581 if (priv->pipes[i].output->bridge)
Laurent Pinchartf40f4e42020-02-26 13:24:58 +0200582 drm_bridge_connector_disable_hpd(connector);
Laurent Pinchart79107f22018-09-23 12:58:15 +0300583 }
Peter Ujfalusi3c596802017-06-02 15:26:35 +0300584}
585
586/*
Rob Clarkcd5351f2011-11-12 12:09:40 -0600587 * drm ioctl funcs
588 */
589
590
591static int ioctl_get_param(struct drm_device *dev, void *data,
592 struct drm_file *file_priv)
593{
Rob Clark5e3b0872012-10-29 09:31:12 +0100594 struct omap_drm_private *priv = dev->dev_private;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600595 struct drm_omap_param *args = data;
596
597 DBG("%p: param=%llu", dev, args->param);
598
599 switch (args->param) {
600 case OMAP_PARAM_CHIPSET_ID:
Rob Clark5e3b0872012-10-29 09:31:12 +0100601 args->value = priv->omaprev;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600602 break;
603 default:
604 DBG("unknown parameter %lld", args->param);
605 return -EINVAL;
606 }
607
608 return 0;
609}
610
Laurent Pinchartef3f4e92015-12-14 22:39:36 +0200611#define OMAP_BO_USER_MASK 0x00ffffff /* flags settable by userspace */
612
Rob Clarkcd5351f2011-11-12 12:09:40 -0600613static int ioctl_gem_new(struct drm_device *dev, void *data,
614 struct drm_file *file_priv)
615{
616 struct drm_omap_gem_new *args = data;
Laurent Pinchartef3f4e92015-12-14 22:39:36 +0200617 u32 flags = args->flags & OMAP_BO_USER_MASK;
618
Rob Clarkf5f94542012-12-04 13:59:12 -0600619 VERB("%p:%p: size=0x%08x, flags=%08x", dev, file_priv,
Laurent Pinchartef3f4e92015-12-14 22:39:36 +0200620 args->size.bytes, flags);
621
622 return omap_gem_new_handle(dev, file_priv, args->size, flags,
623 &args->handle);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600624}
625
Rob Clarkcd5351f2011-11-12 12:09:40 -0600626static int ioctl_gem_info(struct drm_device *dev, void *data,
627 struct drm_file *file_priv)
628{
629 struct drm_omap_gem_info *args = data;
630 struct drm_gem_object *obj;
631 int ret = 0;
632
Rob Clarkf5f94542012-12-04 13:59:12 -0600633 VERB("%p:%p: handle=%d", dev, file_priv, args->handle);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600634
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100635 obj = drm_gem_object_lookup(file_priv, args->handle);
YAMANE Toshiakic7f904b2012-11-14 19:30:38 +0900636 if (!obj)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600637 return -ENOENT;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600638
Rob Clarkf7f9f452011-12-05 19:19:22 -0600639 args->size = omap_gem_mmap_size(obj);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600640 args->offset = omap_gem_mmap_offset(obj);
641
Emil Velikovd742cdd2020-05-15 10:51:06 +0100642 drm_gem_object_put(obj);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600643
644 return ret;
645}
646
Rob Clarkbaa70942013-08-02 13:27:49 -0400647static const struct drm_ioctl_desc ioctls[DRM_COMMAND_END - DRM_COMMAND_BASE] = {
Hemant Hariyani5f6ab8c2016-06-07 13:23:19 -0500648 DRM_IOCTL_DEF_DRV(OMAP_GET_PARAM, ioctl_get_param,
Emil Velikov7042a332019-05-27 09:17:37 +0100649 DRM_RENDER_ALLOW),
Emil Velikov9a671c22019-05-22 16:02:18 +0100650 DRM_IOCTL_DEF_DRV(OMAP_SET_PARAM, drm_invalid_op,
Hemant Hariyani5f6ab8c2016-06-07 13:23:19 -0500651 DRM_AUTH | DRM_MASTER | DRM_ROOT_ONLY),
652 DRM_IOCTL_DEF_DRV(OMAP_GEM_NEW, ioctl_gem_new,
Emil Velikov7042a332019-05-27 09:17:37 +0100653 DRM_RENDER_ALLOW),
Laurent Pinchartd6f544f2017-05-09 01:27:11 +0300654 /* Deprecated, to be removed. */
655 DRM_IOCTL_DEF_DRV(OMAP_GEM_CPU_PREP, drm_noop,
Emil Velikov7042a332019-05-27 09:17:37 +0100656 DRM_RENDER_ALLOW),
Laurent Pinchartd6f544f2017-05-09 01:27:11 +0300657 /* Deprecated, to be removed. */
658 DRM_IOCTL_DEF_DRV(OMAP_GEM_CPU_FINI, drm_noop,
Emil Velikov7042a332019-05-27 09:17:37 +0100659 DRM_RENDER_ALLOW),
Hemant Hariyani5f6ab8c2016-06-07 13:23:19 -0500660 DRM_IOCTL_DEF_DRV(OMAP_GEM_INFO, ioctl_gem_info,
Emil Velikov7042a332019-05-27 09:17:37 +0100661 DRM_RENDER_ALLOW),
Rob Clarkcd5351f2011-11-12 12:09:40 -0600662};
663
664/*
665 * drm driver funcs
666 */
667
Rob Clarkcd5351f2011-11-12 12:09:40 -0600668static int dev_open(struct drm_device *dev, struct drm_file *file)
669{
670 file->driver_priv = NULL;
671
672 DBG("open: dev=%p, file=%p", dev, file);
673
674 return 0;
675}
676
Rob Clarkff4f3872012-01-16 12:51:14 -0600677static const struct file_operations omapdriver_fops = {
Laurent Pinchart222025e2015-01-11 00:02:07 +0200678 .owner = THIS_MODULE,
679 .open = drm_open,
680 .unlocked_ioctl = drm_ioctl,
Tomi Valkeinen9d24159a2017-02-24 13:24:50 +0200681 .compat_ioctl = drm_compat_ioctl,
Laurent Pinchart222025e2015-01-11 00:02:07 +0200682 .release = drm_release,
683 .mmap = omap_gem_mmap,
684 .poll = drm_poll,
685 .read = drm_read,
686 .llseek = noop_llseek,
Rob Clarkff4f3872012-01-16 12:51:14 -0600687};
688
Daniel Vetter70a59dd2020-11-04 11:04:24 +0100689static const struct drm_driver omap_drm_driver = {
Daniel Vetter0424fda2019-06-17 17:39:24 +0200690 .driver_features = DRIVER_MODESET | DRIVER_GEM |
Hemant Hariyani5f6ab8c2016-06-07 13:23:19 -0500691 DRIVER_ATOMIC | DRIVER_RENDER,
Laurent Pinchart222025e2015-01-11 00:02:07 +0200692 .open = dev_open,
Noralf Trønnesef62d302017-12-05 19:25:01 +0100693 .lastclose = drm_fb_helper_lastclose,
Andy Gross6169a1482011-12-15 21:05:17 -0600694#ifdef CONFIG_DEBUG_FS
Laurent Pinchart222025e2015-01-11 00:02:07 +0200695 .debugfs_init = omap_debugfs_init,
Andy Gross6169a1482011-12-15 21:05:17 -0600696#endif
Laurent Pinchart222025e2015-01-11 00:02:07 +0200697 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
698 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
Laurent Pinchart222025e2015-01-11 00:02:07 +0200699 .gem_prime_import = omap_gem_prime_import,
Laurent Pinchart222025e2015-01-11 00:02:07 +0200700 .dumb_create = omap_gem_dumb_create,
701 .dumb_map_offset = omap_gem_dumb_map_offset,
Laurent Pinchart222025e2015-01-11 00:02:07 +0200702 .ioctls = ioctls,
703 .num_ioctls = DRM_OMAP_NUM_IOCTLS,
704 .fops = &omapdriver_fops,
705 .name = DRIVER_NAME,
706 .desc = DRIVER_DESC,
707 .date = DRIVER_DATE,
708 .major = DRIVER_MAJOR,
709 .minor = DRIVER_MINOR,
710 .patchlevel = DRIVER_PATCHLEVEL,
Rob Clarkcd5351f2011-11-12 12:09:40 -0600711};
712
Laurent Pinchart6e471fa2017-05-06 02:57:12 +0300713static const struct soc_device_attribute omapdrm_soc_devices[] = {
714 { .family = "OMAP3", .data = (void *)0x3430 },
715 { .family = "OMAP4", .data = (void *)0x4430 },
716 { .family = "OMAP5", .data = (void *)0x5430 },
717 { .family = "DRA7", .data = (void *)0x0752 },
718 { /* sentinel */ }
719};
720
Laurent Pincharta82f03472018-02-13 14:00:19 +0200721static int omapdrm_init(struct omap_drm_private *priv, struct device *dev)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600722{
Laurent Pinchart6e471fa2017-05-06 02:57:12 +0300723 const struct soc_device_attribute *soc;
Sebastian Reichel85101482020-12-15 12:46:11 +0200724 struct dss_pdata *pdata = dev->platform_data;
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200725 struct drm_device *ddev;
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200726 int ret;
727
Laurent Pincharta82f03472018-02-13 14:00:19 +0200728 DBG("%s", dev_name(dev));
Archit Taneja3a01ab22014-01-02 14:49:51 +0530729
Peter Ujfalusifb96b672018-02-12 11:44:36 +0200730 /* Allocate and initialize the DRM device. */
731 ddev = drm_dev_alloc(&omap_drm_driver, dev);
732 if (IS_ERR(ddev))
733 return PTR_ERR(ddev);
734
735 priv->ddev = ddev;
736 ddev->dev_private = priv;
737
Laurent Pincharta82f03472018-02-13 14:00:19 +0200738 priv->dev = dev;
Sebastian Reichel85101482020-12-15 12:46:11 +0200739 priv->dss = pdata->dss;
Laurent Pinchart50638ae2018-02-13 14:00:42 +0200740 priv->dispc = dispc_get_dispc(priv->dss);
Laurent Pinchart510c74c2017-08-11 16:49:08 +0300741
Tomi Valkeinen05ec6122020-12-15 12:46:27 +0200742 priv->dss->mgr_ops_priv = priv;
Archit Taneja3a01ab22014-01-02 14:49:51 +0530743
Laurent Pinchart6e471fa2017-05-06 02:57:12 +0300744 soc = soc_device_match(omapdrm_soc_devices);
Laurent Pinchart95f227832021-07-28 18:19:34 +0300745 priv->omaprev = soc ? (uintptr_t)soc->data : 0;
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200746 priv->wq = alloc_ordered_workqueue("omapdrm", 0);
747
Daniel Vetter5117bd82018-05-25 19:39:24 +0300748 mutex_init(&priv->list_lock);
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200749 INIT_LIST_HEAD(&priv->obj_list);
750
Peter Ujfalusia7631c42017-11-30 14:12:37 +0200751 /* Get memory bandwidth limits */
Tomi Valkeinendac62bc2020-12-15 12:46:26 +0200752 priv->max_bandwidth = dispc_get_memory_bandwidth_limit(priv->dispc);
Peter Ujfalusia7631c42017-11-30 14:12:37 +0200753
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200754 omap_gem_init(ddev);
755
Benoit Parrot6e422012021-11-17 15:19:25 +0100756 drm_mode_config_init(ddev);
757
758 ret = omap_global_obj_init(ddev);
Benoit Parrotc8fa1e72021-11-17 15:19:23 +0100759 if (ret)
760 goto err_gem_deinit;
761
Benoit Parrot6e422012021-11-17 15:19:25 +0100762 ret = omap_hwoverlays_init(priv);
763 if (ret)
764 goto err_free_priv_obj;
765
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200766 ret = omap_modeset_init(ddev);
767 if (ret) {
Laurent Pincharta82f03472018-02-13 14:00:19 +0200768 dev_err(priv->dev, "omap_modeset_init failed: ret=%d\n", ret);
Benoit Parrotc8fa1e72021-11-17 15:19:23 +0100769 goto err_free_overlays;
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200770 }
771
772 /* Initialize vblank handling, start with all CRTCs disabled. */
Laurent Pinchart2ee76792018-03-05 15:02:22 +0200773 ret = drm_vblank_init(ddev, priv->num_pipes);
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200774 if (ret) {
Laurent Pincharta82f03472018-02-13 14:00:19 +0200775 dev_err(priv->dev, "could not init vblank\n");
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200776 goto err_cleanup_modeset;
777 }
778
Tomi Valkeinenefd1f062018-02-09 09:36:23 +0200779 omap_fbdev_init(ddev);
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200780
781 drm_kms_helper_poll_init(ddev);
Peter Ujfalusi52b9ef22018-02-12 11:44:37 +0200782 omap_modeset_enable_external_hpd(ddev);
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200783
784 /*
785 * Register the DRM device with the core and the connectors with
786 * sysfs.
787 */
788 ret = drm_dev_register(ddev, 0);
789 if (ret)
790 goto err_cleanup_helpers;
791
792 return 0;
793
794err_cleanup_helpers:
Peter Ujfalusi52b9ef22018-02-12 11:44:37 +0200795 omap_modeset_disable_external_hpd(ddev);
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200796 drm_kms_helper_poll_fini(ddev);
Tomi Valkeinenefd1f062018-02-09 09:36:23 +0200797
798 omap_fbdev_fini(ddev);
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200799err_cleanup_modeset:
Laurent Pinchartf40f4e42020-02-26 13:24:58 +0200800 omap_modeset_fini(ddev);
Benoit Parrotc8fa1e72021-11-17 15:19:23 +0100801err_free_overlays:
802 omap_hwoverlays_destroy(priv);
Benoit Parrot6e422012021-11-17 15:19:25 +0100803err_free_priv_obj:
804 omap_global_obj_fini(priv);
Peter Ujfalusifb96b672018-02-12 11:44:36 +0200805err_gem_deinit:
Benoit Parrot6e422012021-11-17 15:19:25 +0100806 drm_mode_config_cleanup(ddev);
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200807 omap_gem_deinit(ddev);
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200808 destroy_workqueue(priv->wq);
Laurent Pinchart2ee76792018-03-05 15:02:22 +0200809 omap_disconnect_pipelines(ddev);
Thomas Zimmermann08bafff2018-06-18 15:07:27 +0200810 drm_dev_put(ddev);
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200811 return ret;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600812}
813
Laurent Pincharta82f03472018-02-13 14:00:19 +0200814static void omapdrm_cleanup(struct omap_drm_private *priv)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600815{
Laurent Pincharta82f03472018-02-13 14:00:19 +0200816 struct drm_device *ddev = priv->ddev;
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200817
Rob Clarkcd5351f2011-11-12 12:09:40 -0600818 DBG("");
Andy Gross5c137792012-03-05 10:48:39 -0600819
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200820 drm_dev_unregister(ddev);
821
Peter Ujfalusi52b9ef22018-02-12 11:44:37 +0200822 omap_modeset_disable_external_hpd(ddev);
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200823 drm_kms_helper_poll_fini(ddev);
824
Tomi Valkeinenefd1f062018-02-09 09:36:23 +0200825 omap_fbdev_fini(ddev);
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200826
Tomi Valkeinen8a54aa92017-03-27 10:02:22 +0300827 drm_atomic_helper_shutdown(ddev);
828
Laurent Pinchartf40f4e42020-02-26 13:24:58 +0200829 omap_modeset_fini(ddev);
Benoit Parrotc8fa1e72021-11-17 15:19:23 +0100830 omap_hwoverlays_destroy(priv);
Benoit Parrot6e422012021-11-17 15:19:25 +0100831 omap_global_obj_fini(priv);
832 drm_mode_config_cleanup(ddev);
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200833 omap_gem_deinit(ddev);
834
Laurent Pinchart2f95bc62016-12-12 11:28:47 +0200835 destroy_workqueue(priv->wq);
Tomi Valkeinen707cf582014-04-02 13:47:43 +0300836
Laurent Pinchart2ee76792018-03-05 15:02:22 +0200837 omap_disconnect_pipelines(ddev);
Peter Ujfalusifb96b672018-02-12 11:44:36 +0200838
Thomas Zimmermann08bafff2018-06-18 15:07:27 +0200839 drm_dev_put(ddev);
Laurent Pincharta82f03472018-02-13 14:00:19 +0200840}
841
842static int pdev_probe(struct platform_device *pdev)
843{
844 struct omap_drm_private *priv;
845 int ret;
846
Tomi Valkeinen63daf4e2019-08-09 13:00:38 +0300847 ret = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
Laurent Pincharta82f03472018-02-13 14:00:19 +0200848 if (ret) {
849 dev_err(&pdev->dev, "Failed to set the DMA mask\n");
850 return ret;
851 }
852
853 /* Allocate and initialize the driver private structure. */
854 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
855 if (!priv)
856 return -ENOMEM;
857
858 platform_set_drvdata(pdev, priv);
859
860 ret = omapdrm_init(priv, &pdev->dev);
861 if (ret < 0)
862 kfree(priv);
863
864 return ret;
865}
866
867static int pdev_remove(struct platform_device *pdev)
868{
869 struct omap_drm_private *priv = platform_get_drvdata(pdev);
870
871 omapdrm_cleanup(priv);
872 kfree(priv);
Daniel Vetterfd3c0252013-12-11 11:34:26 +0100873
Rob Clarkcd5351f2011-11-12 12:09:40 -0600874 return 0;
875}
876
Grygorii Strashko8450c8d2015-02-26 15:57:17 +0200877#ifdef CONFIG_PM_SLEEP
Tomi Valkeinenccd7b5e2014-11-14 15:18:28 +0200878static int omap_drm_suspend(struct device *dev)
879{
Laurent Pincharta82f03472018-02-13 14:00:19 +0200880 struct omap_drm_private *priv = dev_get_drvdata(dev);
881 struct drm_device *drm_dev = priv->ddev;
Tomi Valkeinenccd7b5e2014-11-14 15:18:28 +0200882
Laurent Pinchartd2c53162018-09-04 17:08:33 +0300883 return drm_mode_config_helper_suspend(drm_dev);
Tomi Valkeinenccd7b5e2014-11-14 15:18:28 +0200884}
885
886static int omap_drm_resume(struct device *dev)
887{
Laurent Pincharta82f03472018-02-13 14:00:19 +0200888 struct omap_drm_private *priv = dev_get_drvdata(dev);
889 struct drm_device *drm_dev = priv->ddev;
Tomi Valkeinenccd7b5e2014-11-14 15:18:28 +0200890
Laurent Pinchartd2c53162018-09-04 17:08:33 +0300891 drm_mode_config_helper_resume(drm_dev);
Tomi Valkeinenccd7b5e2014-11-14 15:18:28 +0200892
Laurent Pinchart7fb15c42017-10-13 17:58:58 +0300893 return omap_gem_resume(drm_dev);
Tomi Valkeinenccd7b5e2014-11-14 15:18:28 +0200894}
Andy Grosse78edba2012-12-19 14:53:37 -0600895#endif
896
Grygorii Strashko8450c8d2015-02-26 15:57:17 +0200897static SIMPLE_DEV_PM_OPS(omapdrm_pm_ops, omap_drm_suspend, omap_drm_resume);
898
Tomi Valkeinen6717cd22013-04-10 10:44:00 +0300899static struct platform_driver pdev = {
Laurent Pinchart222025e2015-01-11 00:02:07 +0200900 .driver = {
Tomi Valkeinenf64eafa2017-08-16 12:43:55 +0300901 .name = "omapdrm",
Laurent Pinchart222025e2015-01-11 00:02:07 +0200902 .pm = &omapdrm_pm_ops,
Laurent Pinchart222025e2015-01-11 00:02:07 +0200903 },
904 .probe = pdev_probe,
905 .remove = pdev_remove,
Rob Clarkcd5351f2011-11-12 12:09:40 -0600906};
907
Thierry Redinge1c49bd2015-12-02 17:23:31 +0100908static struct platform_driver * const drivers[] = {
909 &omap_dmm_driver,
910 &pdev,
911};
912
Rob Clarkcd5351f2011-11-12 12:09:40 -0600913static int __init omap_drm_init(void)
914{
Tomi Valkeinen55b68fb2020-12-15 12:46:23 +0200915 int r;
916
Rob Clarkcd5351f2011-11-12 12:09:40 -0600917 DBG("init");
Tomi Valkeinenea7e3a62014-04-02 14:31:50 +0300918
Tomi Valkeinen55b68fb2020-12-15 12:46:23 +0200919 r = omap_dss_init();
920 if (r)
921 return r;
922
923 r = platform_register_drivers(drivers, ARRAY_SIZE(drivers));
924 if (r) {
925 omap_dss_exit();
926 return r;
927 }
928
929 return 0;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600930}
931
932static void __exit omap_drm_fini(void)
933{
934 DBG("fini");
Tomi Valkeinenea7e3a62014-04-02 14:31:50 +0300935
Thierry Redinge1c49bd2015-12-02 17:23:31 +0100936 platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
Tomi Valkeinen55b68fb2020-12-15 12:46:23 +0200937
938 omap_dss_exit();
Rob Clarkcd5351f2011-11-12 12:09:40 -0600939}
940
Tomi Valkeinen55b68fb2020-12-15 12:46:23 +0200941module_init(omap_drm_init);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600942module_exit(omap_drm_fini);
943
944MODULE_AUTHOR("Rob Clark <rob@ti.com>");
Tomi Valkeinen55b68fb2020-12-15 12:46:23 +0200945MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@ti.com>");
Rob Clarkcd5351f2011-11-12 12:09:40 -0600946MODULE_DESCRIPTION("OMAP DRM Display Driver");
947MODULE_ALIAS("platform:" DRIVER_NAME);
948MODULE_LICENSE("GPL v2");