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/>.
21 #include "omap_dmm_tiler.h"
23 /* some hackery because omapdss has an 'enum omap_plane' (which would be
24 * better named omap_plane_id).. and compiler seems unhappy about having
25 * both a 'struct omap_plane' and 'enum omap_plane'
27 #define omap_plane _omap_plane
33 #define to_omap_plane(x) container_of(x, struct omap_plane, base)
36 struct drm_plane base;
37 int id; /* TODO rename omap_plane -> omap_plane_id in omapdss so I can use the enum */
39 struct omap_overlay_info info;
41 /* position/orientation of scanout within the fb: */
42 struct omap_drm_window win;
45 /* last fb that we pinned: */
46 struct drm_framebuffer *pinned_fb;
51 struct omap_drm_irq error_irq;
54 /* update which fb (if any) is pinned for scanout */
55 static int omap_plane_update_pin(struct drm_plane *plane)
57 struct omap_plane *omap_plane = to_omap_plane(plane);
58 struct drm_framebuffer *pinned_fb = omap_plane->pinned_fb;
59 struct drm_framebuffer *fb = omap_plane->enabled ? plane->fb : NULL;
65 DBG("%p -> %p", pinned_fb, fb);
68 drm_framebuffer_reference(fb);
69 ret = omap_framebuffer_pin(fb);
73 omap_crtc_queue_unpin(plane->crtc, pinned_fb);
76 dev_err(plane->dev->dev, "could not swap %p -> %p\n",
77 omap_plane->pinned_fb, fb);
78 drm_framebuffer_unreference(fb);
79 omap_plane->pinned_fb = NULL;
83 omap_plane->pinned_fb = fb;
88 static int omap_plane_setup(struct omap_plane *omap_plane)
90 struct omap_overlay_info *info = &omap_plane->info;
91 struct drm_plane *plane = &omap_plane->base;
92 struct drm_device *dev = plane->dev;
93 struct drm_crtc *crtc = plane->crtc;
96 DBG("%s, enabled=%d", omap_plane->name, omap_plane->enabled);
98 /* if fb has changed, pin new fb: */
99 ret = omap_plane_update_pin(plane);
105 if (!omap_plane->enabled) {
106 dispc_ovl_enable(omap_plane->id, false);
110 /* update scanout: */
111 omap_framebuffer_update_scanout(plane->fb, &omap_plane->win, info);
113 DBG("%dx%d -> %dx%d (%d)", info->width, info->height,
114 info->out_width, info->out_height,
116 DBG("%d,%d %pad %pad", info->pos_x, info->pos_y,
117 &info->paddr, &info->p_uv_addr);
119 dispc_ovl_set_channel_out(omap_plane->id,
120 omap_crtc_channel(crtc));
122 /* and finally, update omapdss: */
123 ret = dispc_ovl_setup(omap_plane->id, info, false,
124 omap_crtc_timings(crtc), false);
126 dev_err(dev->dev, "dispc_ovl_setup failed: %d\n", ret);
130 dispc_ovl_enable(omap_plane->id, true);
137 int omap_plane_mode_set(struct drm_plane *plane,
138 struct drm_crtc *crtc, struct drm_framebuffer *fb,
139 int crtc_x, int crtc_y,
140 unsigned int crtc_w, unsigned int crtc_h,
141 unsigned int src_x, unsigned int src_y,
142 unsigned int src_w, unsigned int src_h)
144 struct omap_plane *omap_plane = to_omap_plane(plane);
145 struct omap_drm_window *win = &omap_plane->win;
147 win->crtc_x = crtc_x;
148 win->crtc_y = crtc_y;
149 win->crtc_w = crtc_w;
150 win->crtc_h = crtc_h;
157 return omap_plane_setup(omap_plane);
160 int omap_plane_set_enable(struct drm_plane *plane, bool enable)
162 struct omap_plane *omap_plane = to_omap_plane(plane);
164 if (enable == omap_plane->enabled)
167 omap_plane->enabled = enable;
168 return omap_plane_setup(omap_plane);
171 static int omap_plane_update(struct drm_plane *plane,
172 struct drm_crtc *crtc, struct drm_framebuffer *fb,
173 int crtc_x, int crtc_y,
174 unsigned int crtc_w, unsigned int crtc_h,
175 uint32_t src_x, uint32_t src_y,
176 uint32_t src_w, uint32_t src_h)
178 struct omap_plane *omap_plane = to_omap_plane(plane);
181 omap_plane->enabled = true;
183 /* omap_plane_mode_set() takes adjusted src */
184 switch (omap_plane->win.rotation & 0xf) {
185 case BIT(DRM_ROTATE_90):
186 case BIT(DRM_ROTATE_270):
192 * We don't need to take a reference to the framebuffer as the DRM core
193 * has already done so for the purpose of setting plane->fb.
198 /* src values are in Q16 fixed point, convert to integer: */
199 ret = omap_plane_mode_set(plane, crtc, fb,
200 crtc_x, crtc_y, crtc_w, crtc_h,
201 src_x >> 16, src_y >> 16,
202 src_w >> 16, src_h >> 16);
206 return omap_crtc_flush(plane->crtc);
209 static int omap_plane_disable(struct drm_plane *plane)
211 struct omap_plane *omap_plane = to_omap_plane(plane);
213 omap_plane->win.rotation = BIT(DRM_ROTATE_0);
214 omap_plane->info.zorder = plane->type == DRM_PLANE_TYPE_PRIMARY
215 ? 0 : omap_plane->id;
217 if (!omap_plane->enabled)
220 /* Disabling a plane never fails. */
221 omap_plane->enabled = false;
222 omap_plane_setup(omap_plane);
224 return omap_crtc_flush(plane->crtc);
227 static void omap_plane_destroy(struct drm_plane *plane)
229 struct omap_plane *omap_plane = to_omap_plane(plane);
231 DBG("%s", omap_plane->name);
233 omap_irq_unregister(plane->dev, &omap_plane->error_irq);
235 drm_plane_cleanup(plane);
240 /* helper to install properties which are common to planes and crtcs */
241 void omap_plane_install_properties(struct drm_plane *plane,
242 struct drm_mode_object *obj)
244 struct drm_device *dev = plane->dev;
245 struct omap_drm_private *priv = dev->dev_private;
248 struct drm_property *prop = dev->mode_config.rotation_property;
250 drm_object_attach_property(obj, prop, 0);
253 drm_object_attach_property(obj, priv->zorder_prop, 0);
256 int omap_plane_set_property(struct drm_plane *plane,
257 struct drm_property *property, uint64_t val)
259 struct omap_plane *omap_plane = to_omap_plane(plane);
260 struct omap_drm_private *priv = plane->dev->dev_private;
263 if (property == plane->dev->mode_config.rotation_property) {
264 DBG("%s: rotation: %02x", omap_plane->name, (uint32_t)val);
265 omap_plane->win.rotation = val;
266 } else if (property == priv->zorder_prop) {
267 DBG("%s: zorder: %02x", omap_plane->name, (uint32_t)val);
268 omap_plane->info.zorder = val;
274 * We're done if the plane is disabled, properties will be applied the
275 * next time it becomes enabled.
277 if (!omap_plane->enabled)
280 ret = omap_plane_setup(omap_plane);
284 return omap_crtc_flush(plane->crtc);
287 static const struct drm_plane_funcs omap_plane_funcs = {
288 .update_plane = omap_plane_update,
289 .disable_plane = omap_plane_disable,
290 .destroy = omap_plane_destroy,
291 .set_property = omap_plane_set_property,
294 static void omap_plane_error_irq(struct omap_drm_irq *irq, uint32_t irqstatus)
296 struct omap_plane *omap_plane =
297 container_of(irq, struct omap_plane, error_irq);
298 DRM_ERROR_RATELIMITED("%s: errors: %08x\n", omap_plane->name,
302 static const char *plane_names[] = {
303 [OMAP_DSS_GFX] = "gfx",
304 [OMAP_DSS_VIDEO1] = "vid1",
305 [OMAP_DSS_VIDEO2] = "vid2",
306 [OMAP_DSS_VIDEO3] = "vid3",
309 static const uint32_t error_irqs[] = {
310 [OMAP_DSS_GFX] = DISPC_IRQ_GFX_FIFO_UNDERFLOW,
311 [OMAP_DSS_VIDEO1] = DISPC_IRQ_VID1_FIFO_UNDERFLOW,
312 [OMAP_DSS_VIDEO2] = DISPC_IRQ_VID2_FIFO_UNDERFLOW,
313 [OMAP_DSS_VIDEO3] = DISPC_IRQ_VID3_FIFO_UNDERFLOW,
316 /* initialize plane */
317 struct drm_plane *omap_plane_init(struct drm_device *dev,
318 int id, enum drm_plane_type type)
320 struct omap_drm_private *priv = dev->dev_private;
321 struct drm_plane *plane;
322 struct omap_plane *omap_plane;
323 struct omap_overlay_info *info;
326 DBG("%s: type=%d", plane_names[id], type);
328 omap_plane = kzalloc(sizeof(*omap_plane), GFP_KERNEL);
330 return ERR_PTR(-ENOMEM);
332 omap_plane->nformats = omap_framebuffer_get_formats(
333 omap_plane->formats, ARRAY_SIZE(omap_plane->formats),
334 dss_feat_get_supported_color_modes(id));
336 omap_plane->name = plane_names[id];
338 plane = &omap_plane->base;
340 omap_plane->error_irq.irqmask = error_irqs[id];
341 omap_plane->error_irq.irq = omap_plane_error_irq;
342 omap_irq_register(dev, &omap_plane->error_irq);
344 ret = drm_universal_plane_init(dev, plane, (1 << priv->num_crtcs) - 1,
345 &omap_plane_funcs, omap_plane->formats,
346 omap_plane->nformats, type);
350 omap_plane_install_properties(plane, &plane->base);
352 /* get our starting configuration, set defaults for parameters
353 * we don't currently use, etc:
355 info = &omap_plane->info;
356 info->rotation_type = OMAP_DSS_ROT_DMA;
357 info->rotation = OMAP_DSS_ROT_0;
358 info->global_alpha = 0xff;
361 /* Set defaults depending on whether we are a CRTC or overlay
363 * TODO add ioctl to give userspace an API to change this.. this
364 * will come in a subsequent patch.
366 if (type == DRM_PLANE_TYPE_PRIMARY)
367 omap_plane->info.zorder = 0;
369 omap_plane->info.zorder = id;
374 omap_irq_unregister(plane->dev, &omap_plane->error_irq);