2 * drivers/gpu/drm/omapdrm/omap_plane.c
4 * Copyright (C) 2011 Texas Instruments
5 * Author: Rob Clark <rob.clark@linaro.org>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 as published by
9 * the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * You should have received a copy of the GNU General Public License along with
17 * this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <drm/drm_atomic_helper.h>
22 #include "omap_dmm_tiler.h"
25 /* some hackery because omapdss has an 'enum omap_plane' (which would be
26 * better named omap_plane_id).. and compiler seems unhappy about having
27 * both a 'struct omap_plane' and 'enum omap_plane'
29 #define omap_plane _omap_plane
35 #define to_omap_plane(x) container_of(x, struct omap_plane, base)
38 struct drm_plane base;
39 int id; /* TODO rename omap_plane -> omap_plane_id in omapdss so I can use the enum */
41 struct omap_overlay_info info;
43 /* position/orientation of scanout within the fb: */
44 struct omap_drm_window win;
47 /* last fb that we pinned: */
48 struct drm_framebuffer *pinned_fb;
53 struct omap_drm_irq error_irq;
56 /* update which fb (if any) is pinned for scanout */
57 static int omap_plane_update_pin(struct drm_plane *plane)
59 struct omap_plane *omap_plane = to_omap_plane(plane);
60 struct drm_framebuffer *pinned_fb = omap_plane->pinned_fb;
61 struct drm_framebuffer *fb = omap_plane->enabled ? plane->fb : NULL;
67 DBG("%p -> %p", pinned_fb, fb);
70 drm_framebuffer_reference(fb);
71 ret = omap_framebuffer_pin(fb);
75 omap_crtc_queue_unpin(plane->crtc, pinned_fb);
78 dev_err(plane->dev->dev, "could not swap %p -> %p\n",
79 omap_plane->pinned_fb, fb);
80 drm_framebuffer_unreference(fb);
81 omap_plane->pinned_fb = NULL;
85 omap_plane->pinned_fb = fb;
90 static int omap_plane_setup(struct omap_plane *omap_plane)
92 struct omap_overlay_info *info = &omap_plane->info;
93 struct drm_plane *plane = &omap_plane->base;
94 struct drm_device *dev = plane->dev;
95 struct drm_crtc *crtc = plane->crtc;
98 DBG("%s, enabled=%d", omap_plane->name, omap_plane->enabled);
100 /* if fb has changed, pin new fb: */
101 ret = omap_plane_update_pin(plane);
107 if (!omap_plane->enabled) {
108 dispc_ovl_enable(omap_plane->id, false);
112 /* update scanout: */
113 omap_framebuffer_update_scanout(plane->fb, &omap_plane->win, info);
115 DBG("%dx%d -> %dx%d (%d)", info->width, info->height,
116 info->out_width, info->out_height,
118 DBG("%d,%d %pad %pad", info->pos_x, info->pos_y,
119 &info->paddr, &info->p_uv_addr);
121 dispc_ovl_set_channel_out(omap_plane->id,
122 omap_crtc_channel(crtc));
124 /* and finally, update omapdss: */
125 ret = dispc_ovl_setup(omap_plane->id, info, false,
126 omap_crtc_timings(crtc), false);
128 dev_err(dev->dev, "dispc_ovl_setup failed: %d\n", ret);
132 dispc_ovl_enable(omap_plane->id, true);
139 int omap_plane_mode_set(struct drm_plane *plane,
140 struct drm_crtc *crtc, struct drm_framebuffer *fb,
141 int crtc_x, int crtc_y,
142 unsigned int crtc_w, unsigned int crtc_h,
143 unsigned int src_x, unsigned int src_y,
144 unsigned int src_w, unsigned int src_h)
146 struct omap_plane *omap_plane = to_omap_plane(plane);
147 struct omap_drm_window *win = &omap_plane->win;
149 win->crtc_x = crtc_x;
150 win->crtc_y = crtc_y;
151 win->crtc_w = crtc_w;
152 win->crtc_h = crtc_h;
159 return omap_plane_setup(omap_plane);
162 int omap_plane_set_enable(struct drm_plane *plane, bool enable)
164 struct omap_plane *omap_plane = to_omap_plane(plane);
166 if (enable == omap_plane->enabled)
169 omap_plane->enabled = enable;
170 return omap_plane_setup(omap_plane);
173 static int omap_plane_update(struct drm_plane *plane,
174 struct drm_crtc *crtc, struct drm_framebuffer *fb,
175 int crtc_x, int crtc_y,
176 unsigned int crtc_w, unsigned int crtc_h,
177 uint32_t src_x, uint32_t src_y,
178 uint32_t src_w, uint32_t src_h)
180 struct omap_plane *omap_plane = to_omap_plane(plane);
183 omap_plane->enabled = true;
185 /* omap_plane_mode_set() takes adjusted src */
186 switch (omap_plane->win.rotation & 0xf) {
187 case BIT(DRM_ROTATE_90):
188 case BIT(DRM_ROTATE_270):
194 * We don't need to take a reference to the framebuffer as the DRM core
195 * has already done so for the purpose of setting plane->fb.
200 /* src values are in Q16 fixed point, convert to integer: */
201 ret = omap_plane_mode_set(plane, crtc, fb,
202 crtc_x, crtc_y, crtc_w, crtc_h,
203 src_x >> 16, src_y >> 16,
204 src_w >> 16, src_h >> 16);
208 return omap_crtc_flush(plane->crtc);
211 static int omap_plane_disable(struct drm_plane *plane)
213 struct omap_plane *omap_plane = to_omap_plane(plane);
215 omap_plane->win.rotation = BIT(DRM_ROTATE_0);
216 omap_plane->info.zorder = plane->type == DRM_PLANE_TYPE_PRIMARY
217 ? 0 : omap_plane->id;
219 if (!omap_plane->enabled)
222 /* Disabling a plane never fails. */
223 omap_plane->enabled = false;
224 omap_plane_setup(omap_plane);
226 return omap_crtc_flush(plane->crtc);
229 static void omap_plane_destroy(struct drm_plane *plane)
231 struct omap_plane *omap_plane = to_omap_plane(plane);
233 DBG("%s", omap_plane->name);
235 omap_irq_unregister(plane->dev, &omap_plane->error_irq);
237 drm_plane_cleanup(plane);
242 /* helper to install properties which are common to planes and crtcs */
243 void omap_plane_install_properties(struct drm_plane *plane,
244 struct drm_mode_object *obj)
246 struct drm_device *dev = plane->dev;
247 struct omap_drm_private *priv = dev->dev_private;
250 struct drm_property *prop = dev->mode_config.rotation_property;
252 drm_object_attach_property(obj, prop, 0);
255 drm_object_attach_property(obj, priv->zorder_prop, 0);
258 int omap_plane_set_property(struct drm_plane *plane,
259 struct drm_property *property, uint64_t val)
261 struct omap_plane *omap_plane = to_omap_plane(plane);
262 struct omap_drm_private *priv = plane->dev->dev_private;
265 if (property == plane->dev->mode_config.rotation_property) {
266 DBG("%s: rotation: %02x", omap_plane->name, (uint32_t)val);
267 omap_plane->win.rotation = val;
268 } else if (property == priv->zorder_prop) {
269 DBG("%s: zorder: %02x", omap_plane->name, (uint32_t)val);
270 omap_plane->info.zorder = val;
276 * We're done if the plane is disabled, properties will be applied the
277 * next time it becomes enabled.
279 if (!omap_plane->enabled)
282 ret = omap_plane_setup(omap_plane);
286 return omap_crtc_flush(plane->crtc);
289 static const struct drm_plane_funcs omap_plane_funcs = {
290 .update_plane = omap_plane_update,
291 .disable_plane = omap_plane_disable,
292 .reset = drm_atomic_helper_plane_reset,
293 .destroy = omap_plane_destroy,
294 .set_property = omap_plane_set_property,
295 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
296 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
299 static void omap_plane_error_irq(struct omap_drm_irq *irq, uint32_t irqstatus)
301 struct omap_plane *omap_plane =
302 container_of(irq, struct omap_plane, error_irq);
303 DRM_ERROR_RATELIMITED("%s: errors: %08x\n", omap_plane->name,
307 static const char *plane_names[] = {
308 [OMAP_DSS_GFX] = "gfx",
309 [OMAP_DSS_VIDEO1] = "vid1",
310 [OMAP_DSS_VIDEO2] = "vid2",
311 [OMAP_DSS_VIDEO3] = "vid3",
314 static const uint32_t error_irqs[] = {
315 [OMAP_DSS_GFX] = DISPC_IRQ_GFX_FIFO_UNDERFLOW,
316 [OMAP_DSS_VIDEO1] = DISPC_IRQ_VID1_FIFO_UNDERFLOW,
317 [OMAP_DSS_VIDEO2] = DISPC_IRQ_VID2_FIFO_UNDERFLOW,
318 [OMAP_DSS_VIDEO3] = DISPC_IRQ_VID3_FIFO_UNDERFLOW,
321 /* initialize plane */
322 struct drm_plane *omap_plane_init(struct drm_device *dev,
323 int id, enum drm_plane_type type)
325 struct omap_drm_private *priv = dev->dev_private;
326 struct drm_plane *plane;
327 struct omap_plane *omap_plane;
328 struct omap_overlay_info *info;
331 DBG("%s: type=%d", plane_names[id], type);
333 omap_plane = kzalloc(sizeof(*omap_plane), GFP_KERNEL);
335 return ERR_PTR(-ENOMEM);
337 omap_plane->nformats = omap_framebuffer_get_formats(
338 omap_plane->formats, ARRAY_SIZE(omap_plane->formats),
339 dss_feat_get_supported_color_modes(id));
341 omap_plane->name = plane_names[id];
343 plane = &omap_plane->base;
345 omap_plane->error_irq.irqmask = error_irqs[id];
346 omap_plane->error_irq.irq = omap_plane_error_irq;
347 omap_irq_register(dev, &omap_plane->error_irq);
349 ret = drm_universal_plane_init(dev, plane, (1 << priv->num_crtcs) - 1,
350 &omap_plane_funcs, omap_plane->formats,
351 omap_plane->nformats, type);
355 omap_plane_install_properties(plane, &plane->base);
357 /* get our starting configuration, set defaults for parameters
358 * we don't currently use, etc:
360 info = &omap_plane->info;
361 info->rotation_type = OMAP_DSS_ROT_DMA;
362 info->rotation = OMAP_DSS_ROT_0;
363 info->global_alpha = 0xff;
366 /* Set defaults depending on whether we are a CRTC or overlay
368 * TODO add ioctl to give userspace an API to change this.. this
369 * will come in a subsequent patch.
371 if (type == DRM_PLANE_TYPE_PRIMARY)
372 omap_plane->info.zorder = 0;
374 omap_plane->info.zorder = id;
379 omap_irq_unregister(plane->dev, &omap_plane->error_irq);