2 * drivers/staging/omapdrm/omap_fb.c
4 * Copyright (C) 2011 Texas Instruments
5 * Author: Rob Clark <rob@ti.com>
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/>.
23 #include "drm_crtc_helper.h"
29 /* per-format info: */
31 enum omap_color_mode dss_format;
32 uint32_t pixel_format;
34 int stride_bpp; /* this times width is stride */
35 int sub_y; /* sub-sample in y dimension */
40 static const struct format formats[] = {
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 */
50 { OMAP_DSS_COLOR_RGB24P, DRM_FORMAT_RGB888, {{3, 1}}, false }, /* RGB24-888 */
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 */
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 },
62 #define to_omap_framebuffer(x) container_of(x, struct omap_framebuffer, base)
64 struct omap_framebuffer {
65 struct drm_framebuffer base;
66 struct drm_gem_object *bo;
69 const struct format *format;
72 static int omap_framebuffer_create_handle(struct drm_framebuffer *fb,
73 struct drm_file *file_priv,
76 struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
77 return drm_gem_handle_create(file_priv, omap_fb->bo, handle);
80 static void omap_framebuffer_destroy(struct drm_framebuffer *fb)
82 struct drm_device *dev = fb->dev;
83 struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
85 DBG("destroy: FB ID: %d (%p)", fb->base.id, fb);
87 drm_framebuffer_cleanup(fb);
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);
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)
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);
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,
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)
123 struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
124 int bpp = fb->bits_per_pixel / 8;
125 unsigned long offset;
127 offset = (x * bpp) + (y * fb->pitches[0]);
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
136 BUG_ON(IS_ERR_OR_NULL(bo_vaddr));
137 *vaddr = bo_vaddr + offset;
140 *paddr = omap_fb->paddr + offset;
141 *screen_width = fb->pitches[0] / bpp;
143 return omap_fb->size - offset;
146 struct drm_gem_object *omap_framebuffer_bo(struct drm_framebuffer *fb)
148 struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
152 /* iterate thru all the connectors, returning ones that are attached
155 struct drm_connector *omap_framebuffer_get_next_connector(
156 struct drm_framebuffer *fb, struct drm_connector *from)
158 struct drm_device *dev = fb->dev;
159 struct list_head *connector_list = &dev->mode_config.connector_list;
160 struct drm_connector *connector = from;
163 return list_first_entry(connector_list, typeof(*from), head);
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) {
179 /* flush an area of the framebuffer (in case of manual update display that
180 * is not automatically flushed)
182 void omap_framebuffer_flush(struct drm_framebuffer *fb,
183 int x, int y, int w, int h)
185 struct drm_connector *connector = NULL;
187 VERB("flush: %d,%d %dx%d, fb=%p", x, y, w, h, fb);
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..
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;
201 omap_connector_flush(connector, cx, cy, cw, ch);
206 struct drm_framebuffer *omap_framebuffer_create(struct drm_device *dev,
207 struct drm_file *file, struct drm_mode_fb_cmd2 *mode_cmd)
209 struct drm_gem_object *bos[4];
210 struct drm_framebuffer *fb;
213 ret = objects_lookup(dev, file, mode_cmd->pixel_format,
214 bos, mode_cmd->handles);
218 fb = omap_framebuffer_init(dev, mode_cmd, bos);
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]);
228 struct drm_framebuffer *omap_framebuffer_init(struct drm_device *dev,
229 struct drm_mode_fb_cmd2 *mode_cmd, struct drm_gem_object **bos)
231 struct omap_framebuffer *omap_fb;
232 struct drm_framebuffer *fb = NULL;
233 const struct format *format = NULL;
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);
240 for (i = 0; i < ARRAY_SIZE(formats); i++) {
241 if (formats[i].pixel_format == mode_cmd->pixel_format) {
242 format = &formats[i];
248 dev_err(dev->dev, "unsupported pixel format: %4.4s\n",
249 (char *)&mode_cmd->pixel_format);
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);
258 omap_fb = kzalloc(sizeof(*omap_fb), GFP_KERNEL);
260 dev_err(dev->dev, "could not allocate fb\n");
266 ret = drm_framebuffer_init(dev, fb, &omap_framebuffer_funcs);
268 dev_err(dev->dev, "framebuffer init failed: %d\n", ret);
272 DBG("create: FB ID: %d (%p)", fb->base.id, fb);
274 size = PAGE_ALIGN(mode_cmd->pitches[0] * mode_cmd->height);
276 if (size > bos[0]->size) {
277 dev_err(dev->dev, "provided buffer object is too small!\n");
282 omap_fb->bo = bos[0];
283 omap_fb->size = size;
285 ret = omap_gem_get_paddr(bos[0], &omap_fb->paddr, true);
287 dev_err(dev->dev, "could not map (paddr)!\n");
291 drm_helper_mode_fill_fb_struct(fb, mode_cmd);
297 omap_framebuffer_destroy(fb);