blob: 2899435cad6e4322b05b3cfb5e90719590e6cab9 [file] [log] [blame]
Rob Clarkbb5c2d92012-01-16 12:51:16 -06001/*
Andrew F. Davisbb5cdf82017-12-05 14:29:31 -06002 * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
Rob Clarkbb5c2d92012-01-16 12:51:16 -06003 * Author: Rob Clark <rob.clark@linaro.org>
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
Tomi Valkeinenc423bc82015-06-16 14:54:51 +030018#include <drm/drm_atomic.h>
Laurent Pinchart69a12262015-03-05 21:38:16 +020019#include <drm/drm_atomic_helper.h>
Laurent Pinchartde8e4102015-03-05 13:39:56 +020020#include <drm/drm_plane_helper.h>
Laurent Pinchart69a12262015-03-05 21:38:16 +020021
Rob Clark3c810c62012-08-15 15:18:01 -050022#include "omap_dmm_tiler.h"
Laurent Pinchart2d278f52015-03-05 21:31:37 +020023#include "omap_drv.h"
Rob Clarkbb5c2d92012-01-16 12:51:16 -060024
Rob Clarkbb5c2d92012-01-16 12:51:16 -060025/*
26 * plane funcs
27 */
28
29#define to_omap_plane(x) container_of(x, struct omap_plane, base)
30
31struct omap_plane {
32 struct drm_plane base;
Jyri Sarha694c99c2017-03-24 16:47:54 +020033 enum omap_plane_id id;
Rob Clarkf5f94542012-12-04 13:59:12 -060034 const char *name;
Rob Clarka890e662012-03-05 10:48:31 -060035};
Rob Clarkbb5c2d92012-01-16 12:51:16 -060036
Tomi Valkeinen11ffd032015-05-29 11:05:37 +030037static int omap_plane_prepare_fb(struct drm_plane *plane,
Chris Wilson18320402016-08-18 19:00:16 +010038 struct drm_plane_state *new_state)
Tomi Valkeinen11ffd032015-05-29 11:05:37 +030039{
Maarten Lankhorst844f9112015-09-02 10:42:40 +020040 if (!new_state->fb)
41 return 0;
42
43 return omap_framebuffer_pin(new_state->fb);
Tomi Valkeinen11ffd032015-05-29 11:05:37 +030044}
45
46static void omap_plane_cleanup_fb(struct drm_plane *plane,
Chris Wilson18320402016-08-18 19:00:16 +010047 struct drm_plane_state *old_state)
Tomi Valkeinen11ffd032015-05-29 11:05:37 +030048{
Maarten Lankhorst844f9112015-09-02 10:42:40 +020049 if (old_state->fb)
50 omap_framebuffer_unpin(old_state->fb);
Tomi Valkeinen11ffd032015-05-29 11:05:37 +030051}
52
53static void omap_plane_atomic_update(struct drm_plane *plane,
54 struct drm_plane_state *old_state)
Laurent Pinchartafc34932015-03-06 18:35:16 +020055{
Tomi Valkeinen9f759222015-11-05 18:39:52 +020056 struct omap_drm_private *priv = plane->dev->dev_private;
Laurent Pinchartedc72552015-03-06 19:18:56 +020057 struct omap_plane *omap_plane = to_omap_plane(plane);
58 struct drm_plane_state *state = plane->state;
Laurent Pinchartfb730c92015-03-06 19:00:18 +020059 struct omap_overlay_info info;
Rob Clark9a0774e2012-01-16 12:51:17 -060060 int ret;
Rob Clarkbb5c2d92012-01-16 12:51:16 -060061
Laurent Pinchartedc72552015-03-06 19:18:56 +020062 DBG("%s, crtc=%p fb=%p", omap_plane->name, state->crtc, state->fb);
Rob Clarkf5f94542012-12-04 13:59:12 -060063
Laurent Pinchartfb730c92015-03-06 19:00:18 +020064 memset(&info, 0, sizeof(info));
Tomi Valkeinen517a8a952017-05-03 14:14:27 +030065 info.rotation_type = OMAP_DSS_ROT_NONE;
Tomi Valkeinen0bd97c42017-05-16 11:05:09 +030066 info.rotation = DRM_MODE_ROTATE_0;
Laurent Pinchartfb730c92015-03-06 19:00:18 +020067 info.global_alpha = 0xff;
Laurent Pinchartba527c12017-05-09 01:27:12 +030068 info.zorder = state->zpos;
Laurent Pinchartfb730c92015-03-06 19:00:18 +020069
Rob Clarkf5f94542012-12-04 13:59:12 -060070 /* update scanout: */
Tomi Valkeinen218ed532017-05-15 13:40:08 +030071 omap_framebuffer_update_scanout(state->fb, state, &info);
Rob Clark9a0774e2012-01-16 12:51:17 -060072
Laurent Pinchartfb730c92015-03-06 19:00:18 +020073 DBG("%dx%d -> %dx%d (%d)", info.width, info.height,
74 info.out_width, info.out_height,
75 info.screen_width);
76 DBG("%d,%d %pad %pad", info.pos_x, info.pos_y,
77 &info.paddr, &info.p_uv_addr);
Rob Clarkf5f94542012-12-04 13:59:12 -060078
Rob Clarkf5f94542012-12-04 13:59:12 -060079 /* and finally, update omapdss: */
Laurent Pinchart50638ae2018-02-13 14:00:42 +020080 ret = priv->dispc_ops->ovl_setup(priv->dispc, omap_plane->id, &info,
Tomi Valkeinen49a30572017-02-17 12:30:07 +020081 omap_crtc_timings(state->crtc), false,
82 omap_crtc_channel(state->crtc));
Tomi Valkeinencfb73f22016-06-10 12:44:31 +030083 if (ret) {
84 dev_err(plane->dev->dev, "Failed to setup plane %s\n",
85 omap_plane->name);
Laurent Pinchart50638ae2018-02-13 14:00:42 +020086 priv->dispc_ops->ovl_enable(priv->dispc, omap_plane->id, false);
Tomi Valkeinend9157df2015-05-29 11:03:15 +030087 return;
Tomi Valkeinen794a65f2015-05-29 11:06:07 +030088 }
Rob Clarkf5f94542012-12-04 13:59:12 -060089
Laurent Pinchart50638ae2018-02-13 14:00:42 +020090 priv->dispc_ops->ovl_enable(priv->dispc, omap_plane->id, true);
Laurent Pinchartde8e4102015-03-05 13:39:56 +020091}
92
Laurent Pinchartde8e4102015-03-05 13:39:56 +020093static void omap_plane_atomic_disable(struct drm_plane *plane,
94 struct drm_plane_state *old_state)
Rob Clarkbb5c2d92012-01-16 12:51:16 -060095{
Tomi Valkeinen9f759222015-11-05 18:39:52 +020096 struct omap_drm_private *priv = plane->dev->dev_private;
Rob Clark3c810c62012-08-15 15:18:01 -050097 struct omap_plane *omap_plane = to_omap_plane(plane);
Laurent Pinchart2debab92015-01-12 22:38:16 +020098
Robert Fossc2c446a2017-05-19 16:50:17 -040099 plane->state->rotation = DRM_MODE_ROTATE_0;
Laurent Pinchartba527c12017-05-09 01:27:12 +0300100 plane->state->zpos = plane->type == DRM_PLANE_TYPE_PRIMARY
Laurent Pinchartafc34932015-03-06 18:35:16 +0200101 ? 0 : omap_plane->id;
Laurent Pinchart82e588552015-01-12 23:56:57 +0200102
Laurent Pinchart50638ae2018-02-13 14:00:42 +0200103 priv->dispc_ops->ovl_enable(priv->dispc, omap_plane->id, false);
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600104}
105
Tomi Valkeinenc423bc82015-06-16 14:54:51 +0300106static int omap_plane_atomic_check(struct drm_plane *plane,
107 struct drm_plane_state *state)
108{
109 struct drm_crtc_state *crtc_state;
110
Tomi Valkeinen70dd2a62016-06-10 12:50:53 +0300111 if (!state->fb)
Tomi Valkeinenc423bc82015-06-16 14:54:51 +0300112 return 0;
113
Tomi Valkeinen70dd2a62016-06-10 12:50:53 +0300114 /* crtc should only be NULL when disabling (i.e., !state->fb) */
115 if (WARN_ON(!state->crtc))
116 return 0;
117
118 crtc_state = drm_atomic_get_existing_crtc_state(state->state, state->crtc);
119 /* we should have a crtc state if the plane is attached to a crtc */
120 if (WARN_ON(!crtc_state))
121 return 0;
Tomi Valkeinenc423bc82015-06-16 14:54:51 +0300122
Tomi Valkeinenaaf76422016-06-10 12:50:39 +0300123 if (!crtc_state->enable)
124 return 0;
125
Tomi Valkeinenc423bc82015-06-16 14:54:51 +0300126 if (state->crtc_x < 0 || state->crtc_y < 0)
127 return -EINVAL;
128
129 if (state->crtc_x + state->crtc_w > crtc_state->adjusted_mode.hdisplay)
130 return -EINVAL;
131
132 if (state->crtc_y + state->crtc_h > crtc_state->adjusted_mode.vdisplay)
133 return -EINVAL;
134
Robert Fossc2c446a2017-05-19 16:50:17 -0400135 if (state->rotation != DRM_MODE_ROTATE_0 &&
Tomi Valkeinen70dd2a62016-06-10 12:50:53 +0300136 !omap_framebuffer_supports_rotation(state->fb))
137 return -EINVAL;
Tomi Valkeinenbfeece52015-08-27 13:09:22 +0300138
Tomi Valkeinenc423bc82015-06-16 14:54:51 +0300139 return 0;
140}
141
Laurent Pinchartde8e4102015-03-05 13:39:56 +0200142static const struct drm_plane_helper_funcs omap_plane_helper_funcs = {
143 .prepare_fb = omap_plane_prepare_fb,
144 .cleanup_fb = omap_plane_cleanup_fb,
Tomi Valkeinenc423bc82015-06-16 14:54:51 +0300145 .atomic_check = omap_plane_atomic_check,
Laurent Pinchartde8e4102015-03-05 13:39:56 +0200146 .atomic_update = omap_plane_atomic_update,
147 .atomic_disable = omap_plane_atomic_disable,
148};
149
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600150static void omap_plane_destroy(struct drm_plane *plane)
151{
152 struct omap_plane *omap_plane = to_omap_plane(plane);
Rob Clarkf5f94542012-12-04 13:59:12 -0600153
154 DBG("%s", omap_plane->name);
155
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600156 drm_plane_cleanup(plane);
Rob Clarkf5f94542012-12-04 13:59:12 -0600157
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600158 kfree(omap_plane);
159}
160
Rob Clark3c810c62012-08-15 15:18:01 -0500161/* helper to install properties which are common to planes and crtcs */
162void omap_plane_install_properties(struct drm_plane *plane,
163 struct drm_mode_object *obj)
164{
165 struct drm_device *dev = plane->dev;
166 struct omap_drm_private *priv = dev->dev_private;
Rob Clark3c810c62012-08-15 15:18:01 -0500167
Rob Clarkc2a6a552012-10-25 17:14:13 -0500168 if (priv->has_dmm) {
Ville Syrjälä0da88db2016-09-26 19:30:52 +0300169 if (!plane->rotation_property)
170 drm_plane_create_rotation_property(plane,
Robert Fossc2c446a2017-05-19 16:50:17 -0400171 DRM_MODE_ROTATE_0,
172 DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_90 |
173 DRM_MODE_ROTATE_180 | DRM_MODE_ROTATE_270 |
174 DRM_MODE_REFLECT_X | DRM_MODE_REFLECT_Y);
Laurent Pincharte2cd09b2015-03-06 17:16:43 +0200175
Ville Syrjälä0da88db2016-09-26 19:30:52 +0300176 /* Attach the rotation property also to the crtc object */
177 if (plane->rotation_property && obj != &plane->base)
178 drm_object_attach_property(obj, plane->rotation_property,
Robert Fossc2c446a2017-05-19 16:50:17 -0400179 DRM_MODE_ROTATE_0);
Rob Clark3c810c62012-08-15 15:18:01 -0500180 }
Andre Renaud8451b5a2012-08-15 15:18:02 -0500181
Laurent Pincharte2cd09b2015-03-06 17:16:43 +0200182 drm_object_attach_property(obj, priv->zorder_prop, 0);
Rob Clark3c810c62012-08-15 15:18:01 -0500183}
184
Tomi Valkeinene07323c2015-12-15 11:33:23 +0200185static void omap_plane_reset(struct drm_plane *plane)
186{
187 struct omap_plane *omap_plane = to_omap_plane(plane);
Tomi Valkeinene07323c2015-12-15 11:33:23 +0200188
Laurent Pinchartd9802782017-05-09 01:27:13 +0300189 drm_atomic_helper_plane_reset(plane);
190 if (!plane->state)
Tomi Valkeinene07323c2015-12-15 11:33:23 +0200191 return;
192
193 /*
Laurent Pinchartba527c12017-05-09 01:27:12 +0300194 * Set the zpos default depending on whether we are a primary or overlay
Tomi Valkeinene07323c2015-12-15 11:33:23 +0200195 * plane.
196 */
Laurent Pinchartd9802782017-05-09 01:27:13 +0300197 plane->state->zpos = plane->type == DRM_PLANE_TYPE_PRIMARY
198 ? 0 : omap_plane->id;
Tomi Valkeinene07323c2015-12-15 11:33:23 +0200199}
200
Laurent Pinchartafc34932015-03-06 18:35:16 +0200201static int omap_plane_atomic_set_property(struct drm_plane *plane,
202 struct drm_plane_state *state,
203 struct drm_property *property,
Laurent Pinchartdfe9cfc2018-02-11 15:07:33 +0200204 u64 val)
Laurent Pinchartafc34932015-03-06 18:35:16 +0200205{
Rob Clark3c810c62012-08-15 15:18:01 -0500206 struct omap_drm_private *priv = plane->dev->dev_private;
Rob Clark3c810c62012-08-15 15:18:01 -0500207
Laurent Pinchartafc34932015-03-06 18:35:16 +0200208 if (property == priv->zorder_prop)
Laurent Pinchartba527c12017-05-09 01:27:12 +0300209 state->zpos = val;
Laurent Pinchartafc34932015-03-06 18:35:16 +0200210 else
Laurent Pincharta42133a2015-01-17 19:09:26 +0200211 return -EINVAL;
Rob Clark3c810c62012-08-15 15:18:01 -0500212
Laurent Pinchartafc34932015-03-06 18:35:16 +0200213 return 0;
214}
Laurent Pincharta42133a2015-01-17 19:09:26 +0200215
Laurent Pinchartafc34932015-03-06 18:35:16 +0200216static int omap_plane_atomic_get_property(struct drm_plane *plane,
217 const struct drm_plane_state *state,
218 struct drm_property *property,
Laurent Pinchartdfe9cfc2018-02-11 15:07:33 +0200219 u64 *val)
Laurent Pinchartafc34932015-03-06 18:35:16 +0200220{
221 struct omap_drm_private *priv = plane->dev->dev_private;
Laurent Pincharta42133a2015-01-17 19:09:26 +0200222
Laurent Pinchartafc34932015-03-06 18:35:16 +0200223 if (property == priv->zorder_prop)
Laurent Pinchartba527c12017-05-09 01:27:12 +0300224 *val = state->zpos;
Laurent Pinchartafc34932015-03-06 18:35:16 +0200225 else
226 return -EINVAL;
227
228 return 0;
Rob Clark3c810c62012-08-15 15:18:01 -0500229}
230
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600231static const struct drm_plane_funcs omap_plane_funcs = {
Laurent Pinchartcef77d42015-03-05 21:50:00 +0200232 .update_plane = drm_atomic_helper_update_plane,
233 .disable_plane = drm_atomic_helper_disable_plane,
Laurent Pinchartafc34932015-03-06 18:35:16 +0200234 .reset = omap_plane_reset,
Laurent Pinchart222025e2015-01-11 00:02:07 +0200235 .destroy = omap_plane_destroy,
Laurent Pinchartd9802782017-05-09 01:27:13 +0300236 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
237 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
Laurent Pinchartafc34932015-03-06 18:35:16 +0200238 .atomic_set_property = omap_plane_atomic_set_property,
239 .atomic_get_property = omap_plane_atomic_get_property,
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600240};
241
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200242static const char *plane_id_to_name[] = {
Laurent Pinchart222025e2015-01-11 00:02:07 +0200243 [OMAP_DSS_GFX] = "gfx",
244 [OMAP_DSS_VIDEO1] = "vid1",
245 [OMAP_DSS_VIDEO2] = "vid2",
246 [OMAP_DSS_VIDEO3] = "vid3",
Rob Clarkf5f94542012-12-04 13:59:12 -0600247};
248
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200249static const enum omap_plane_id plane_idx_to_id[] = {
250 OMAP_DSS_GFX,
251 OMAP_DSS_VIDEO1,
252 OMAP_DSS_VIDEO2,
253 OMAP_DSS_VIDEO3,
254};
255
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600256/* initialize plane */
257struct drm_plane *omap_plane_init(struct drm_device *dev,
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200258 int idx, enum drm_plane_type type,
Tomi Valkeinene43f2c32016-12-02 16:07:11 +0200259 u32 possible_crtcs)
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600260{
Tomi Valkeinen9f759222015-11-05 18:39:52 +0200261 struct omap_drm_private *priv = dev->dev_private;
Laurent Pinchart50638ae2018-02-13 14:00:42 +0200262 unsigned int num_planes = priv->dispc_ops->get_num_ovls(priv->dispc);
Laurent Pinchartef6b0e02015-01-11 00:11:18 +0200263 struct drm_plane *plane;
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600264 struct omap_plane *omap_plane;
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200265 enum omap_plane_id id;
Laurent Pinchartef6b0e02015-01-11 00:11:18 +0200266 int ret;
Tomi Valkeineneecad432017-05-04 11:27:49 +0300267 u32 nformats;
268 const u32 *formats;
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600269
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200270 if (WARN_ON(idx >= ARRAY_SIZE(plane_idx_to_id)))
271 return ERR_PTR(-EINVAL);
272
273 id = plane_idx_to_id[idx];
274
275 DBG("%s: type=%d", plane_id_to_name[id], type);
Rob Clarkb33f34d2012-03-05 10:48:35 -0600276
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600277 omap_plane = kzalloc(sizeof(*omap_plane), GFP_KERNEL);
Joe Perches78110bb2013-02-11 09:41:29 -0800278 if (!omap_plane)
Laurent Pinchartfb9a35f2015-01-11 16:30:44 +0200279 return ERR_PTR(-ENOMEM);
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600280
Laurent Pinchart50638ae2018-02-13 14:00:42 +0200281 formats = priv->dispc_ops->ovl_get_color_modes(priv->dispc, id);
Tomi Valkeineneecad432017-05-04 11:27:49 +0300282 for (nformats = 0; formats[nformats]; ++nformats)
283 ;
Rob Clarkf5f94542012-12-04 13:59:12 -0600284 omap_plane->id = id;
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200285 omap_plane->name = plane_id_to_name[id];
Rob Clarkf5f94542012-12-04 13:59:12 -0600286
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600287 plane = &omap_plane->base;
288
Tomi Valkeinene43f2c32016-12-02 16:07:11 +0200289 ret = drm_universal_plane_init(dev, plane, possible_crtcs,
Tomi Valkeineneecad432017-05-04 11:27:49 +0300290 &omap_plane_funcs, formats,
Ben Widawskye6fc3b62017-07-23 20:46:38 -0700291 nformats, NULL, type, NULL);
Laurent Pinchartef6b0e02015-01-11 00:11:18 +0200292 if (ret < 0)
293 goto error;
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600294
Laurent Pinchartde8e4102015-03-05 13:39:56 +0200295 drm_plane_helper_add(plane, &omap_plane_helper_funcs);
296
Rob Clark3c810c62012-08-15 15:18:01 -0500297 omap_plane_install_properties(plane, &plane->base);
Laurent Pinchartdff6c242017-05-09 01:27:14 +0300298 drm_plane_create_zpos_property(plane, 0, 0, num_planes - 1);
Rob Clark3c810c62012-08-15 15:18:01 -0500299
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600300 return plane;
Laurent Pinchartef6b0e02015-01-11 00:11:18 +0200301
302error:
Jyri Sarhae8e13b12017-03-24 16:47:55 +0200303 dev_err(dev->dev, "%s(): could not create plane: %s\n",
304 __func__, plane_id_to_name[id]);
305
Laurent Pinchartef6b0e02015-01-11 00:11:18 +0200306 kfree(omap_plane);
307 return NULL;
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600308}