]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/omapdrm/omap_plane.c
staging: drm/omap: get supported color formats from ovl
[karo-tx-linux.git] / drivers / staging / omapdrm / omap_plane.c
1 /*
2  * drivers/staging/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 "omap_drv.h"
21
22 /* some hackery because omapdss has an 'enum omap_plane' (which would be
23  * better named omap_plane_id).. and compiler seems unhappy about having
24  * both a 'struct omap_plane' and 'enum omap_plane'
25  */
26 #define omap_plane _omap_plane
27
28 /*
29  * plane funcs
30  */
31
32 #define to_omap_plane(x) container_of(x, struct omap_plane, base)
33
34 struct omap_plane {
35         struct drm_plane base;
36         struct omap_overlay *ovl;
37         struct omap_overlay_info info;
38
39         /* Source values, converted to integers because we don't support
40          * fractional positions:
41          */
42         unsigned int src_x, src_y;
43
44         /* last fb that we pinned: */
45         struct drm_framebuffer *pinned_fb;
46
47         uint32_t nformats;
48         uint32_t formats[32];
49 };
50
51 /* push changes down to dss2 */
52 static int commit(struct drm_plane *plane)
53 {
54         struct drm_device *dev = plane->dev;
55         struct omap_plane *omap_plane = to_omap_plane(plane);
56         struct omap_overlay *ovl = omap_plane->ovl;
57         struct omap_overlay_info *info = &omap_plane->info;
58         int ret;
59
60         DBG("%s", ovl->name);
61         DBG("%dx%d -> %dx%d (%d)", info->width, info->height, info->out_width,
62                         info->out_height, info->screen_width);
63         DBG("%d,%d %08x %08x", info->pos_x, info->pos_y,
64                         info->paddr, info->p_uv_addr);
65
66         /* NOTE: do we want to do this at all here, or just wait
67          * for dpms(ON) since other CRTC's may not have their mode
68          * set yet, so fb dimensions may still change..
69          */
70         ret = ovl->set_overlay_info(ovl, info);
71         if (ret) {
72                 dev_err(dev->dev, "could not set overlay info\n");
73                 return ret;
74         }
75
76         /* our encoder doesn't necessarily get a commit() after this, in
77          * particular in the dpms() and mode_set_base() cases, so force the
78          * manager to update:
79          *
80          * could this be in the encoder somehow?
81          */
82         if (ovl->manager) {
83                 ret = ovl->manager->apply(ovl->manager);
84                 if (ret) {
85                         dev_err(dev->dev, "could not apply settings\n");
86                         return ret;
87                 }
88         }
89
90         if (ovl->is_enabled(ovl)) {
91                 omap_framebuffer_flush(plane->fb, info->pos_x, info->pos_y,
92                                 info->out_width, info->out_height);
93         }
94
95         return 0;
96 }
97
98 /* when CRTC that we are attached to has potentially changed, this checks
99  * if we are attached to proper manager, and if necessary updates.
100  */
101 static void update_manager(struct drm_plane *plane)
102 {
103         struct omap_drm_private *priv = plane->dev->dev_private;
104         struct omap_plane *omap_plane = to_omap_plane(plane);
105         struct omap_overlay *ovl = omap_plane->ovl;
106         struct omap_overlay_manager *mgr = NULL;
107         int i;
108
109         if (plane->crtc) {
110                 for (i = 0; i < priv->num_encoders; i++) {
111                         struct drm_encoder *encoder = priv->encoders[i];
112                         if (encoder->crtc == plane->crtc) {
113                                 mgr = omap_encoder_get_manager(encoder);
114                                 break;
115                         }
116                 }
117         }
118
119         if (ovl->manager != mgr) {
120                 bool enabled = ovl->is_enabled(ovl);
121
122                 /* don't switch things around with enabled overlays: */
123                 if (enabled)
124                         omap_plane_dpms(plane, DRM_MODE_DPMS_OFF);
125
126                 if (ovl->manager) {
127                         DBG("disconnecting %s from %s", ovl->name,
128                                         ovl->manager->name);
129                         ovl->unset_manager(ovl);
130                 }
131
132                 if (mgr) {
133                         DBG("connecting %s to %s", ovl->name, mgr->name);
134                         ovl->set_manager(ovl, mgr);
135                 }
136
137                 if (enabled && mgr)
138                         omap_plane_dpms(plane, DRM_MODE_DPMS_ON);
139         }
140 }
141
142 /* update which fb (if any) is pinned for scanout */
143 static int update_pin(struct drm_plane *plane, struct drm_framebuffer *fb)
144 {
145         struct omap_plane *omap_plane = to_omap_plane(plane);
146         int ret = 0;
147
148         if (omap_plane->pinned_fb != fb) {
149                 if (omap_plane->pinned_fb)
150                         omap_framebuffer_unpin(omap_plane->pinned_fb);
151                 omap_plane->pinned_fb = fb;
152                 if (fb)
153                         ret = omap_framebuffer_pin(fb);
154         }
155
156         return ret;
157 }
158
159 /* update parameters that are dependent on the framebuffer dimensions and
160  * position within the fb that this plane scans out from. This is called
161  * when framebuffer or x,y base may have changed.
162  */
163 static void update_scanout(struct drm_plane *plane)
164 {
165         struct omap_plane *omap_plane = to_omap_plane(plane);
166         struct omap_overlay_info *info = &omap_plane->info;
167         int ret;
168
169         ret = update_pin(plane, plane->fb);
170         if (ret) {
171                 dev_err(plane->dev->dev,
172                         "could not pin fb: %d\n", ret);
173                 omap_plane_dpms(plane, DRM_MODE_DPMS_OFF);
174                 return;
175         }
176
177         omap_framebuffer_update_scanout(plane->fb,
178                         omap_plane->src_x, omap_plane->src_y, info);
179
180         DBG("%s: %d,%d: %08x %08x (%d)", omap_plane->ovl->name,
181                         omap_plane->src_x, omap_plane->src_y,
182                         (u32)info->paddr, (u32)info->p_uv_addr,
183                         info->screen_width);
184 }
185
186 int omap_plane_mode_set(struct drm_plane *plane,
187                 struct drm_crtc *crtc, struct drm_framebuffer *fb,
188                 int crtc_x, int crtc_y,
189                 unsigned int crtc_w, unsigned int crtc_h,
190                 uint32_t src_x, uint32_t src_y,
191                 uint32_t src_w, uint32_t src_h)
192 {
193         struct omap_plane *omap_plane = to_omap_plane(plane);
194
195         /* src values are in Q16 fixed point, convert to integer: */
196         src_x = src_x >> 16;
197         src_y = src_y >> 16;
198         src_w = src_w >> 16;
199         src_h = src_h >> 16;
200
201         omap_plane->info.pos_x = crtc_x;
202         omap_plane->info.pos_y = crtc_y;
203         omap_plane->info.out_width = crtc_w;
204         omap_plane->info.out_height = crtc_h;
205         omap_plane->info.width = src_w;
206         omap_plane->info.height = src_h;
207         omap_plane->src_x = src_x;
208         omap_plane->src_y = src_y;
209
210         /* note: this is done after this fxn returns.. but if we need
211          * to do a commit/update_scanout, etc before this returns we
212          * need the current value.
213          */
214         plane->fb = fb;
215         plane->crtc = crtc;
216
217         update_scanout(plane);
218         update_manager(plane);
219
220         return 0;
221 }
222
223 static int omap_plane_update(struct drm_plane *plane,
224                 struct drm_crtc *crtc, struct drm_framebuffer *fb,
225                 int crtc_x, int crtc_y,
226                 unsigned int crtc_w, unsigned int crtc_h,
227                 uint32_t src_x, uint32_t src_y,
228                 uint32_t src_w, uint32_t src_h)
229 {
230         omap_plane_mode_set(plane, crtc, fb, crtc_x, crtc_y, crtc_w, crtc_h,
231                         src_x, src_y, src_w, src_h);
232         return omap_plane_dpms(plane, DRM_MODE_DPMS_ON);
233 }
234
235 static int omap_plane_disable(struct drm_plane *plane)
236 {
237         return omap_plane_dpms(plane, DRM_MODE_DPMS_OFF);
238 }
239
240 static void omap_plane_destroy(struct drm_plane *plane)
241 {
242         struct omap_plane *omap_plane = to_omap_plane(plane);
243         DBG("%s", omap_plane->ovl->name);
244         omap_plane_disable(plane);
245         drm_plane_cleanup(plane);
246         kfree(omap_plane);
247 }
248
249 int omap_plane_dpms(struct drm_plane *plane, int mode)
250 {
251         struct omap_plane *omap_plane = to_omap_plane(plane);
252         struct omap_overlay *ovl = omap_plane->ovl;
253         int r;
254
255         DBG("%s: %d", omap_plane->ovl->name, mode);
256
257         if (mode == DRM_MODE_DPMS_ON) {
258                 update_scanout(plane);
259                 r = commit(plane);
260                 if (!r)
261                         r = ovl->enable(ovl);
262         } else {
263                 r = ovl->disable(ovl);
264                 update_pin(plane, NULL);
265         }
266
267         return r;
268 }
269
270 static const struct drm_plane_funcs omap_plane_funcs = {
271                 .update_plane = omap_plane_update,
272                 .disable_plane = omap_plane_disable,
273                 .destroy = omap_plane_destroy,
274 };
275
276 /* initialize plane */
277 struct drm_plane *omap_plane_init(struct drm_device *dev,
278                 struct omap_overlay *ovl, unsigned int possible_crtcs,
279                 bool priv)
280 {
281         struct drm_plane *plane = NULL;
282         struct omap_plane *omap_plane;
283
284         DBG("%s: possible_crtcs=%08x, priv=%d", ovl->name,
285                         possible_crtcs, priv);
286
287         omap_plane = kzalloc(sizeof(*omap_plane), GFP_KERNEL);
288         if (!omap_plane) {
289                 dev_err(dev->dev, "could not allocate plane\n");
290                 goto fail;
291         }
292
293         omap_plane->nformats = omap_framebuffer_get_formats(
294                         omap_plane->formats, ARRAY_SIZE(omap_plane->formats),
295                         ovl->supported_modes);
296         omap_plane->ovl = ovl;
297         plane = &omap_plane->base;
298
299         drm_plane_init(dev, plane, possible_crtcs, &omap_plane_funcs,
300                         omap_plane->formats, omap_plane->nformats, priv);
301
302         /* get our starting configuration, set defaults for parameters
303          * we don't currently use, etc:
304          */
305         ovl->get_overlay_info(ovl, &omap_plane->info);
306         omap_plane->info.rotation_type = OMAP_DSS_ROT_DMA;
307         omap_plane->info.rotation = OMAP_DSS_ROT_0;
308         omap_plane->info.global_alpha = 0xff;
309         omap_plane->info.mirror = 0;
310         omap_plane->info.mirror = 0;
311
312         /* Set defaults depending on whether we are a CRTC or overlay
313          * layer.
314          * TODO add ioctl to give userspace an API to change this.. this
315          * will come in a subsequent patch.
316          */
317         if (priv)
318                 omap_plane->info.zorder = 0;
319         else
320                 omap_plane->info.zorder = 1;
321
322         update_manager(plane);
323
324         return plane;
325
326 fail:
327         if (plane) {
328                 omap_plane_destroy(plane);
329         }
330         return NULL;
331 }