staging: drm/omap: drm API update: addfb2
[linux-2.6.git] / drivers / staging / omapdrm / omap_fb.c
1 /*
2  * drivers/staging/omapdrm/omap_fb.c
3  *
4  * Copyright (C) 2011 Texas Instruments
5  * Author: Rob Clark <rob@ti.com>
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 #include "drm_crtc.h"
23 #include "drm_crtc_helper.h"
24
25 /*
26  * framebuffer funcs
27  */
28
29 /* per-format info: */
30 struct format {
31         enum omap_color_mode dss_format;
32         uint32_t pixel_format;
33         struct {
34                 int stride_bpp;           /* this times width is stride */
35                 int sub_y;                /* sub-sample in y dimension */
36         } planes[4];
37         bool yuv;
38 };
39
40 static const struct format formats[] = {
41         /* 16bpp [A]RGB: */
42         { OMAP_DSS_COLOR_RGB16,       DRM_FORMAT_RGB565,   {{2, 1}}, false }, /* RGB16-565 */
43         { OMAP_DSS_COLOR_RGB12U,      DRM_FORMAT_RGBX4444, {{2, 1}}, false }, /* RGB12x-4444 */
44         { OMAP_DSS_COLOR_RGBX16,      DRM_FORMAT_XRGB4444, {{2, 1}}, false }, /* xRGB12-4444 */
45         { OMAP_DSS_COLOR_RGBA16,      DRM_FORMAT_RGBA4444, {{2, 1}}, false }, /* RGBA12-4444 */
46         { OMAP_DSS_COLOR_ARGB16,      DRM_FORMAT_ARGB4444, {{2, 1}}, false }, /* ARGB16-4444 */
47         { OMAP_DSS_COLOR_XRGB16_1555, DRM_FORMAT_XRGB1555, {{2, 1}}, false }, /* xRGB15-1555 */
48         { OMAP_DSS_COLOR_ARGB16_1555, DRM_FORMAT_ARGB1555, {{2, 1}}, false }, /* ARGB16-1555 */
49         /* 24bpp RGB: */
50         { OMAP_DSS_COLOR_RGB24P,      DRM_FORMAT_RGB888,   {{3, 1}}, false }, /* RGB24-888 */
51         /* 32bpp [A]RGB: */
52         { OMAP_DSS_COLOR_RGBX32,      DRM_FORMAT_RGBX8888, {{4, 1}}, false }, /* RGBx24-8888 */
53         { OMAP_DSS_COLOR_RGB24U,      DRM_FORMAT_XRGB8888, {{4, 1}}, false }, /* xRGB24-8888 */
54         { OMAP_DSS_COLOR_RGBA32,      DRM_FORMAT_RGBA8888, {{4, 1}}, false }, /* RGBA32-8888 */
55         { OMAP_DSS_COLOR_ARGB32,      DRM_FORMAT_ARGB8888, {{4, 1}}, false }, /* ARGB32-8888 */
56         /* YUV: */
57         { OMAP_DSS_COLOR_NV12,        DRM_FORMAT_NV12,     {{1, 1}, {1, 2}}, true },
58         { OMAP_DSS_COLOR_YUV2,        DRM_FORMAT_YUYV,     {{2, 1}}, true },
59         { OMAP_DSS_COLOR_UYVY,        DRM_FORMAT_UYVY,     {{2, 1}}, true },
60 };
61
62 #define to_omap_framebuffer(x) container_of(x, struct omap_framebuffer, base)
63
64 struct omap_framebuffer {
65         struct drm_framebuffer base;
66         struct drm_gem_object *bo;
67         int size;
68         dma_addr_t paddr;
69         const struct format *format;
70 };
71
72 static int omap_framebuffer_create_handle(struct drm_framebuffer *fb,
73                 struct drm_file *file_priv,
74                 unsigned int *handle)
75 {
76         struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
77     return drm_gem_handle_create(file_priv, omap_fb->bo, handle);
78 }
79
80 static void omap_framebuffer_destroy(struct drm_framebuffer *fb)
81 {
82         struct drm_device *dev = fb->dev;
83         struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
84
85         DBG("destroy: FB ID: %d (%p)", fb->base.id, fb);
86
87         drm_framebuffer_cleanup(fb);
88
89         if (omap_fb->bo) {
90                 if (omap_fb->paddr && omap_gem_put_paddr(omap_fb->bo))
91                         dev_err(dev->dev, "could not unmap!\n");
92                 drm_gem_object_unreference_unlocked(omap_fb->bo);
93         }
94
95         kfree(omap_fb);
96 }
97
98 static int omap_framebuffer_dirty(struct drm_framebuffer *fb,
99                 struct drm_file *file_priv, unsigned flags, unsigned color,
100                 struct drm_clip_rect *clips, unsigned num_clips)
101 {
102         int i;
103
104         for (i = 0; i < num_clips; i++) {
105                 omap_framebuffer_flush(fb, clips[i].x1, clips[i].y1,
106                                         clips[i].x2 - clips[i].x1,
107                                         clips[i].y2 - clips[i].y1);
108         }
109
110         return 0;
111 }
112
113 static const struct drm_framebuffer_funcs omap_framebuffer_funcs = {
114         .create_handle = omap_framebuffer_create_handle,
115         .destroy = omap_framebuffer_destroy,
116         .dirty = omap_framebuffer_dirty,
117 };
118
119 /* returns the buffer size */
120 int omap_framebuffer_get_buffer(struct drm_framebuffer *fb, int x, int y,
121                 void **vaddr, dma_addr_t *paddr, unsigned int *screen_width)
122 {
123         struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
124         int bpp = fb->bits_per_pixel / 8;
125         unsigned long offset;
126
127         offset = (x * bpp) + (y * fb->pitches[0]);
128
129         if (vaddr) {
130                 void *bo_vaddr = omap_gem_vaddr(omap_fb->bo);
131                 /* note: we can only count on having a vaddr for buffers that
132                  * are allocated physically contiguously to begin with (ie.
133                  * dma_alloc_coherent()).  But this should be ok because it
134                  * is only used by legacy fbdev
135                  */
136                 BUG_ON(IS_ERR_OR_NULL(bo_vaddr));
137                 *vaddr = bo_vaddr + offset;
138         }
139
140         *paddr = omap_fb->paddr + offset;
141         *screen_width = fb->pitches[0] / bpp;
142
143         return omap_fb->size - offset;
144 }
145
146 struct drm_gem_object *omap_framebuffer_bo(struct drm_framebuffer *fb)
147 {
148         struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
149         return omap_fb->bo;
150 }
151
152 /* iterate thru all the connectors, returning ones that are attached
153  * to the same fb..
154  */
155 struct drm_connector *omap_framebuffer_get_next_connector(
156                 struct drm_framebuffer *fb, struct drm_connector *from)
157 {
158         struct drm_device *dev = fb->dev;
159         struct list_head *connector_list = &dev->mode_config.connector_list;
160         struct drm_connector *connector = from;
161
162         if (!from) {
163                 return list_first_entry(connector_list, typeof(*from), head);
164         }
165
166         list_for_each_entry_from(connector, connector_list, head) {
167                 if (connector != from) {
168                         struct drm_encoder *encoder = connector->encoder;
169                         struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
170                         if (crtc && crtc->fb == fb) {
171                                 return connector;
172                         }
173                 }
174         }
175
176         return NULL;
177 }
178
179 /* flush an area of the framebuffer (in case of manual update display that
180  * is not automatically flushed)
181  */
182 void omap_framebuffer_flush(struct drm_framebuffer *fb,
183                 int x, int y, int w, int h)
184 {
185         struct drm_connector *connector = NULL;
186
187         VERB("flush: %d,%d %dx%d, fb=%p", x, y, w, h, fb);
188
189         while ((connector = omap_framebuffer_get_next_connector(fb, connector))) {
190                 /* only consider connectors that are part of a chain */
191                 if (connector->encoder && connector->encoder->crtc) {
192                         /* TODO: maybe this should propagate thru the crtc who
193                          * could do the coordinate translation..
194                          */
195                         struct drm_crtc *crtc = connector->encoder->crtc;
196                         int cx = max(0, x - crtc->x);
197                         int cy = max(0, y - crtc->y);
198                         int cw = w + (x - crtc->x) - cx;
199                         int ch = h + (y - crtc->y) - cy;
200
201                         omap_connector_flush(connector, cx, cy, cw, ch);
202                 }
203         }
204 }
205
206 struct drm_framebuffer *omap_framebuffer_create(struct drm_device *dev,
207                 struct drm_file *file, struct drm_mode_fb_cmd2 *mode_cmd)
208 {
209         struct drm_gem_object *bos[4];
210         struct drm_framebuffer *fb;
211         int ret;
212
213         ret = objects_lookup(dev, file, mode_cmd->pixel_format,
214                         bos, mode_cmd->handles);
215         if (ret)
216                 return ERR_PTR(ret);
217
218         fb = omap_framebuffer_init(dev, mode_cmd, bos);
219         if (IS_ERR(fb)) {
220                 int i, n = drm_format_num_planes(mode_cmd->pixel_format);
221                 for (i = 0; i < n; i++)
222                         drm_gem_object_unreference_unlocked(bos[i]);
223                 return fb;
224         }
225         return fb;
226 }
227
228 struct drm_framebuffer *omap_framebuffer_init(struct drm_device *dev,
229                 struct drm_mode_fb_cmd2 *mode_cmd, struct drm_gem_object **bos)
230 {
231         struct omap_framebuffer *omap_fb;
232         struct drm_framebuffer *fb = NULL;
233         const struct format *format = NULL;
234         int i, size, ret;
235
236         DBG("create framebuffer: dev=%p, mode_cmd=%p (%dx%d@%4.4s)",
237                         dev, mode_cmd, mode_cmd->width, mode_cmd->height,
238                         (char *)&mode_cmd->pixel_format);
239
240         for (i = 0; i < ARRAY_SIZE(formats); i++) {
241                 if (formats[i].pixel_format == mode_cmd->pixel_format) {
242                         format = &formats[i];
243                         break;
244                 }
245         }
246
247         if (!format) {
248                 dev_err(dev->dev, "unsupported pixel format: %4.4s\n",
249                                 (char *)&mode_cmd->pixel_format);
250                 ret = -EINVAL;
251                 goto fail;
252         }
253
254         /* in case someone tries to feed us a completely bogus stride: */
255         mode_cmd->pitches[0] = align_pitch(mode_cmd->pitches[0],
256                         mode_cmd->width, format->planes[0].stride_bpp);
257
258         omap_fb = kzalloc(sizeof(*omap_fb), GFP_KERNEL);
259         if (!omap_fb) {
260                 dev_err(dev->dev, "could not allocate fb\n");
261                 ret = -ENOMEM;
262                 goto fail;
263         }
264
265         fb = &omap_fb->base;
266         ret = drm_framebuffer_init(dev, fb, &omap_framebuffer_funcs);
267         if (ret) {
268                 dev_err(dev->dev, "framebuffer init failed: %d\n", ret);
269                 goto fail;
270         }
271
272         DBG("create: FB ID: %d (%p)", fb->base.id, fb);
273
274         size = PAGE_ALIGN(mode_cmd->pitches[0] * mode_cmd->height);
275
276         if (size > bos[0]->size) {
277                 dev_err(dev->dev, "provided buffer object is too small!\n");
278                 ret = -EINVAL;
279                 goto fail;
280         }
281
282         omap_fb->bo = bos[0];
283         omap_fb->size = size;
284
285         ret = omap_gem_get_paddr(bos[0], &omap_fb->paddr, true);
286         if (ret) {
287                 dev_err(dev->dev, "could not map (paddr)!\n");
288                 goto fail;
289         }
290
291         drm_helper_mode_fill_fb_struct(fb, mode_cmd);
292
293         return fb;
294
295 fail:
296         if (fb) {
297                 omap_framebuffer_destroy(fb);
298         }
299         return ERR_PTR(ret);
300 }