]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/gpu/drm/omapdrm/omap_plane.c
drm: omapdrm: Wire up atomic state object scaffolding
[karo-tx-linux.git] / drivers / gpu / drm / omapdrm / omap_plane.c
1 /*
2  * drivers/gpu/drm/omapdrm/omap_plane.c
3  *
4  * Copyright (C) 2011 Texas Instruments
5  * Author: Rob Clark <rob.clark@linaro.org>
6  *
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.
10  *
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
14  * more details.
15  *
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/>.
18  */
19
20 #include <drm/drm_atomic_helper.h>
21
22 #include "omap_dmm_tiler.h"
23 #include "omap_drv.h"
24
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'
28  */
29 #define omap_plane _omap_plane
30
31 /*
32  * plane funcs
33  */
34
35 #define to_omap_plane(x) container_of(x, struct omap_plane, base)
36
37 struct omap_plane {
38         struct drm_plane base;
39         int id;  /* TODO rename omap_plane -> omap_plane_id in omapdss so I can use the enum */
40         const char *name;
41         struct omap_overlay_info info;
42
43         /* position/orientation of scanout within the fb: */
44         struct omap_drm_window win;
45         bool enabled;
46
47         /* last fb that we pinned: */
48         struct drm_framebuffer *pinned_fb;
49
50         uint32_t nformats;
51         uint32_t formats[32];
52
53         struct omap_drm_irq error_irq;
54 };
55
56 /* update which fb (if any) is pinned for scanout */
57 static int omap_plane_update_pin(struct drm_plane *plane)
58 {
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;
62         int ret = 0;
63
64         if (pinned_fb == fb)
65                 return 0;
66
67         DBG("%p -> %p", pinned_fb, fb);
68
69         if (fb) {
70                 drm_framebuffer_reference(fb);
71                 ret = omap_framebuffer_pin(fb);
72         }
73
74         if (pinned_fb)
75                 omap_crtc_queue_unpin(plane->crtc, pinned_fb);
76
77         if (ret) {
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;
82                 return ret;
83         }
84
85         omap_plane->pinned_fb = fb;
86
87         return 0;
88 }
89
90 static int omap_plane_setup(struct omap_plane *omap_plane)
91 {
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;
96         int ret;
97
98         DBG("%s, enabled=%d", omap_plane->name, omap_plane->enabled);
99
100         /* if fb has changed, pin new fb: */
101         ret = omap_plane_update_pin(plane);
102         if (ret)
103                 return ret;
104
105         dispc_runtime_get();
106
107         if (!omap_plane->enabled) {
108                 dispc_ovl_enable(omap_plane->id, false);
109                 goto done;
110         }
111
112         /* update scanout: */
113         omap_framebuffer_update_scanout(plane->fb, &omap_plane->win, info);
114
115         DBG("%dx%d -> %dx%d (%d)", info->width, info->height,
116                         info->out_width, info->out_height,
117                         info->screen_width);
118         DBG("%d,%d %pad %pad", info->pos_x, info->pos_y,
119                         &info->paddr, &info->p_uv_addr);
120
121         dispc_ovl_set_channel_out(omap_plane->id,
122                                   omap_crtc_channel(crtc));
123
124         /* and finally, update omapdss: */
125         ret = dispc_ovl_setup(omap_plane->id, info, false,
126                               omap_crtc_timings(crtc), false);
127         if (ret) {
128                 dev_err(dev->dev, "dispc_ovl_setup failed: %d\n", ret);
129                 goto done;
130         }
131
132         dispc_ovl_enable(omap_plane->id, true);
133
134 done:
135         dispc_runtime_put();
136         return ret;
137 }
138
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)
145 {
146         struct omap_plane *omap_plane = to_omap_plane(plane);
147         struct omap_drm_window *win = &omap_plane->win;
148
149         win->crtc_x = crtc_x;
150         win->crtc_y = crtc_y;
151         win->crtc_w = crtc_w;
152         win->crtc_h = crtc_h;
153
154         win->src_x = src_x;
155         win->src_y = src_y;
156         win->src_w = src_w;
157         win->src_h = src_h;
158
159         return omap_plane_setup(omap_plane);
160 }
161
162 int omap_plane_set_enable(struct drm_plane *plane, bool enable)
163 {
164         struct omap_plane *omap_plane = to_omap_plane(plane);
165
166         if (enable == omap_plane->enabled)
167                 return 0;
168
169         omap_plane->enabled = enable;
170         return omap_plane_setup(omap_plane);
171 }
172
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)
179 {
180         struct omap_plane *omap_plane = to_omap_plane(plane);
181         int ret;
182
183         omap_plane->enabled = true;
184
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):
189                 swap(src_w, src_h);
190                 break;
191         }
192
193         /*
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.
196          */
197         plane->fb = fb;
198         plane->crtc = crtc;
199
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);
205         if (ret < 0)
206                 return ret;
207
208         return omap_crtc_flush(plane->crtc);
209 }
210
211 static int omap_plane_disable(struct drm_plane *plane)
212 {
213         struct omap_plane *omap_plane = to_omap_plane(plane);
214
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;
218
219         if (!omap_plane->enabled)
220                 return 0;
221
222         /* Disabling a plane never fails. */
223         omap_plane->enabled = false;
224         omap_plane_setup(omap_plane);
225
226         return omap_crtc_flush(plane->crtc);
227 }
228
229 static void omap_plane_destroy(struct drm_plane *plane)
230 {
231         struct omap_plane *omap_plane = to_omap_plane(plane);
232
233         DBG("%s", omap_plane->name);
234
235         omap_irq_unregister(plane->dev, &omap_plane->error_irq);
236
237         drm_plane_cleanup(plane);
238
239         kfree(omap_plane);
240 }
241
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)
245 {
246         struct drm_device *dev = plane->dev;
247         struct omap_drm_private *priv = dev->dev_private;
248
249         if (priv->has_dmm) {
250                 struct drm_property *prop = dev->mode_config.rotation_property;
251
252                 drm_object_attach_property(obj, prop, 0);
253         }
254
255         drm_object_attach_property(obj, priv->zorder_prop, 0);
256 }
257
258 int omap_plane_set_property(struct drm_plane *plane,
259                 struct drm_property *property, uint64_t val)
260 {
261         struct omap_plane *omap_plane = to_omap_plane(plane);
262         struct omap_drm_private *priv = plane->dev->dev_private;
263         int ret;
264
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;
271         } else {
272                 return -EINVAL;
273         }
274
275         /*
276          * We're done if the plane is disabled, properties will be applied the
277          * next time it becomes enabled.
278          */
279         if (!omap_plane->enabled)
280                 return 0;
281
282         ret = omap_plane_setup(omap_plane);
283         if (ret < 0)
284                 return ret;
285
286         return omap_crtc_flush(plane->crtc);
287 }
288
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,
297 };
298
299 static void omap_plane_error_irq(struct omap_drm_irq *irq, uint32_t irqstatus)
300 {
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,
304                 irqstatus);
305 }
306
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",
312 };
313
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,
319 };
320
321 /* initialize plane */
322 struct drm_plane *omap_plane_init(struct drm_device *dev,
323                 int id, enum drm_plane_type type)
324 {
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;
329         int ret;
330
331         DBG("%s: type=%d", plane_names[id], type);
332
333         omap_plane = kzalloc(sizeof(*omap_plane), GFP_KERNEL);
334         if (!omap_plane)
335                 return ERR_PTR(-ENOMEM);
336
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));
340         omap_plane->id = id;
341         omap_plane->name = plane_names[id];
342
343         plane = &omap_plane->base;
344
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);
348
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);
352         if (ret < 0)
353                 goto error;
354
355         omap_plane_install_properties(plane, &plane->base);
356
357         /* get our starting configuration, set defaults for parameters
358          * we don't currently use, etc:
359          */
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;
364         info->mirror = 0;
365
366         /* Set defaults depending on whether we are a CRTC or overlay
367          * layer.
368          * TODO add ioctl to give userspace an API to change this.. this
369          * will come in a subsequent patch.
370          */
371         if (type == DRM_PLANE_TYPE_PRIMARY)
372                 omap_plane->info.zorder = 0;
373         else
374                 omap_plane->info.zorder = id;
375
376         return plane;
377
378 error:
379         omap_irq_unregister(plane->dev, &omap_plane->error_irq);
380         kfree(omap_plane);
381         return NULL;
382 }