Merge branch 'akpm' (patches from Andrew Morton)
[linux-2.6.git] / drivers / video / omap2 / vrfb.c
blobf346b02eee1d84c2d8da77b15e3d64c72aa4fa17
1 /*
2 * VRFB Rotation Engine
4 * Copyright (C) 2009 Nokia Corporation
5 * Author: Tomi Valkeinen <tomi.valkeinen@nokia.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 /*#define DEBUG*/
23 #include <linux/err.h>
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/ioport.h>
27 #include <linux/io.h>
28 #include <linux/bitops.h>
29 #include <linux/mutex.h>
30 #include <linux/platform_device.h>
32 #include <video/omapvrfb.h>
34 #ifdef DEBUG
35 #define DBG(format, ...) pr_debug("VRFB: " format, ## __VA_ARGS__)
36 #else
37 #define DBG(format, ...)
38 #endif
40 #define SMS_ROT_CONTROL(context) (0x0 + 0x10 * context)
41 #define SMS_ROT_SIZE(context) (0x4 + 0x10 * context)
42 #define SMS_ROT_PHYSICAL_BA(context) (0x8 + 0x10 * context)
43 #define SMS_ROT_VIRT_BASE(rot) (0x1000000 * (rot))
45 #define OMAP_VRFB_SIZE (2048 * 2048 * 4)
47 #define VRFB_PAGE_WIDTH_EXP 5 /* Assuming SDRAM pagesize= 1024 */
48 #define VRFB_PAGE_HEIGHT_EXP 5 /* 1024 = 2^5 * 2^5 */
49 #define VRFB_PAGE_WIDTH (1 << VRFB_PAGE_WIDTH_EXP)
50 #define VRFB_PAGE_HEIGHT (1 << VRFB_PAGE_HEIGHT_EXP)
51 #define SMS_IMAGEHEIGHT_OFFSET 16
52 #define SMS_IMAGEWIDTH_OFFSET 0
53 #define SMS_PH_OFFSET 8
54 #define SMS_PW_OFFSET 4
55 #define SMS_PS_OFFSET 0
57 /* bitmap of reserved contexts */
58 static unsigned long ctx_map;
60 struct vrfb_ctx {
61 u32 base;
62 u32 physical_ba;
63 u32 control;
64 u32 size;
67 static DEFINE_MUTEX(ctx_lock);
70 * Access to this happens from client drivers or the PM core after wake-up.
71 * For the first case we require locking at the driver level, for the second
72 * we don't need locking, since no drivers will run until after the wake-up
73 * has finished.
76 static void __iomem *vrfb_base;
78 static int num_ctxs;
79 static struct vrfb_ctx *ctxs;
81 static bool vrfb_loaded;
83 static void omap2_sms_write_rot_control(u32 val, unsigned ctx)
85 __raw_writel(val, vrfb_base + SMS_ROT_CONTROL(ctx));
88 static void omap2_sms_write_rot_size(u32 val, unsigned ctx)
90 __raw_writel(val, vrfb_base + SMS_ROT_SIZE(ctx));
93 static void omap2_sms_write_rot_physical_ba(u32 val, unsigned ctx)
95 __raw_writel(val, vrfb_base + SMS_ROT_PHYSICAL_BA(ctx));
98 static inline void restore_hw_context(int ctx)
100 omap2_sms_write_rot_control(ctxs[ctx].control, ctx);
101 omap2_sms_write_rot_size(ctxs[ctx].size, ctx);
102 omap2_sms_write_rot_physical_ba(ctxs[ctx].physical_ba, ctx);
105 static u32 get_image_width_roundup(u16 width, u8 bytespp)
107 unsigned long stride = width * bytespp;
108 unsigned long ceil_pages_per_stride = (stride / VRFB_PAGE_WIDTH) +
109 (stride % VRFB_PAGE_WIDTH != 0);
111 return ceil_pages_per_stride * VRFB_PAGE_WIDTH / bytespp;
115 * This the extra space needed in the VRFB physical area for VRFB to safely wrap
116 * any memory accesses to the invisible part of the virtual view to the physical
117 * area.
119 static inline u32 get_extra_physical_size(u16 image_width_roundup, u8 bytespp)
121 return (OMAP_VRFB_LINE_LEN - image_width_roundup) * VRFB_PAGE_HEIGHT *
122 bytespp;
125 void omap_vrfb_restore_context(void)
127 int i;
128 unsigned long map = ctx_map;
130 for (i = ffs(map); i; i = ffs(map)) {
131 /* i=1..32 */
132 i--;
133 map &= ~(1 << i);
134 restore_hw_context(i);
138 void omap_vrfb_adjust_size(u16 *width, u16 *height,
139 u8 bytespp)
141 *width = ALIGN(*width * bytespp, VRFB_PAGE_WIDTH) / bytespp;
142 *height = ALIGN(*height, VRFB_PAGE_HEIGHT);
144 EXPORT_SYMBOL(omap_vrfb_adjust_size);
146 u32 omap_vrfb_min_phys_size(u16 width, u16 height, u8 bytespp)
148 unsigned long image_width_roundup = get_image_width_roundup(width,
149 bytespp);
151 if (image_width_roundup > OMAP_VRFB_LINE_LEN)
152 return 0;
154 return (width * height * bytespp) + get_extra_physical_size(
155 image_width_roundup, bytespp);
157 EXPORT_SYMBOL(omap_vrfb_min_phys_size);
159 u16 omap_vrfb_max_height(u32 phys_size, u16 width, u8 bytespp)
161 unsigned long image_width_roundup = get_image_width_roundup(width,
162 bytespp);
163 unsigned long height;
164 unsigned long extra;
166 if (image_width_roundup > OMAP_VRFB_LINE_LEN)
167 return 0;
169 extra = get_extra_physical_size(image_width_roundup, bytespp);
171 if (phys_size < extra)
172 return 0;
174 height = (phys_size - extra) / (width * bytespp);
176 /* Virtual views provided by VRFB are limited to 2048x2048. */
177 return min_t(unsigned long, height, 2048);
179 EXPORT_SYMBOL(omap_vrfb_max_height);
181 void omap_vrfb_setup(struct vrfb *vrfb, unsigned long paddr,
182 u16 width, u16 height,
183 unsigned bytespp, bool yuv_mode)
185 unsigned pixel_size_exp;
186 u16 vrfb_width;
187 u16 vrfb_height;
188 u8 ctx = vrfb->context;
189 u32 size;
190 u32 control;
192 DBG("omapfb_set_vrfb(%d, %lx, %dx%d, %d, %d)\n", ctx, paddr,
193 width, height, bytespp, yuv_mode);
195 /* For YUV2 and UYVY modes VRFB needs to handle pixels a bit
196 * differently. See TRM. */
197 if (yuv_mode) {
198 bytespp *= 2;
199 width /= 2;
202 if (bytespp == 4)
203 pixel_size_exp = 2;
204 else if (bytespp == 2)
205 pixel_size_exp = 1;
206 else {
207 BUG();
208 return;
211 vrfb_width = ALIGN(width * bytespp, VRFB_PAGE_WIDTH) / bytespp;
212 vrfb_height = ALIGN(height, VRFB_PAGE_HEIGHT);
214 DBG("vrfb w %u, h %u bytespp %d\n", vrfb_width, vrfb_height, bytespp);
216 size = vrfb_width << SMS_IMAGEWIDTH_OFFSET;
217 size |= vrfb_height << SMS_IMAGEHEIGHT_OFFSET;
219 control = pixel_size_exp << SMS_PS_OFFSET;
220 control |= VRFB_PAGE_WIDTH_EXP << SMS_PW_OFFSET;
221 control |= VRFB_PAGE_HEIGHT_EXP << SMS_PH_OFFSET;
223 ctxs[ctx].physical_ba = paddr;
224 ctxs[ctx].size = size;
225 ctxs[ctx].control = control;
227 omap2_sms_write_rot_physical_ba(paddr, ctx);
228 omap2_sms_write_rot_size(size, ctx);
229 omap2_sms_write_rot_control(control, ctx);
231 DBG("vrfb offset pixels %d, %d\n",
232 vrfb_width - width, vrfb_height - height);
234 vrfb->xres = width;
235 vrfb->yres = height;
236 vrfb->xoffset = vrfb_width - width;
237 vrfb->yoffset = vrfb_height - height;
238 vrfb->bytespp = bytespp;
239 vrfb->yuv_mode = yuv_mode;
241 EXPORT_SYMBOL(omap_vrfb_setup);
243 int omap_vrfb_map_angle(struct vrfb *vrfb, u16 height, u8 rot)
245 unsigned long size = height * OMAP_VRFB_LINE_LEN * vrfb->bytespp;
247 vrfb->vaddr[rot] = ioremap_wc(vrfb->paddr[rot], size);
249 if (!vrfb->vaddr[rot]) {
250 printk(KERN_ERR "vrfb: ioremap failed\n");
251 return -ENOMEM;
254 DBG("ioremapped vrfb area %d of size %lu into %p\n", rot, size,
255 vrfb->vaddr[rot]);
257 return 0;
259 EXPORT_SYMBOL(omap_vrfb_map_angle);
261 void omap_vrfb_release_ctx(struct vrfb *vrfb)
263 int rot;
264 int ctx = vrfb->context;
266 if (ctx == 0xff)
267 return;
269 DBG("release ctx %d\n", ctx);
271 mutex_lock(&ctx_lock);
273 BUG_ON(!(ctx_map & (1 << ctx)));
275 clear_bit(ctx, &ctx_map);
277 for (rot = 0; rot < 4; ++rot) {
278 if (vrfb->paddr[rot]) {
279 release_mem_region(vrfb->paddr[rot], OMAP_VRFB_SIZE);
280 vrfb->paddr[rot] = 0;
284 vrfb->context = 0xff;
286 mutex_unlock(&ctx_lock);
288 EXPORT_SYMBOL(omap_vrfb_release_ctx);
290 int omap_vrfb_request_ctx(struct vrfb *vrfb)
292 int rot;
293 u32 paddr;
294 u8 ctx;
295 int r;
297 DBG("request ctx\n");
299 mutex_lock(&ctx_lock);
301 for (ctx = 0; ctx < num_ctxs; ++ctx)
302 if ((ctx_map & (1 << ctx)) == 0)
303 break;
305 if (ctx == num_ctxs) {
306 pr_err("vrfb: no free contexts\n");
307 r = -EBUSY;
308 goto out;
311 DBG("found free ctx %d\n", ctx);
313 set_bit(ctx, &ctx_map);
315 memset(vrfb, 0, sizeof(*vrfb));
317 vrfb->context = ctx;
319 for (rot = 0; rot < 4; ++rot) {
320 paddr = ctxs[ctx].base + SMS_ROT_VIRT_BASE(rot);
321 if (!request_mem_region(paddr, OMAP_VRFB_SIZE, "vrfb")) {
322 pr_err("vrfb: failed to reserve VRFB "
323 "area for ctx %d, rotation %d\n",
324 ctx, rot * 90);
325 omap_vrfb_release_ctx(vrfb);
326 r = -ENOMEM;
327 goto out;
330 vrfb->paddr[rot] = paddr;
332 DBG("VRFB %d/%d: %lx\n", ctx, rot*90, vrfb->paddr[rot]);
335 r = 0;
336 out:
337 mutex_unlock(&ctx_lock);
338 return r;
340 EXPORT_SYMBOL(omap_vrfb_request_ctx);
342 bool omap_vrfb_supported(void)
344 return vrfb_loaded;
346 EXPORT_SYMBOL(omap_vrfb_supported);
348 static int __init vrfb_probe(struct platform_device *pdev)
350 struct resource *mem;
351 int i;
353 /* first resource is the register res, the rest are vrfb contexts */
355 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
356 vrfb_base = devm_ioremap_resource(&pdev->dev, mem);
357 if (IS_ERR(vrfb_base))
358 return PTR_ERR(vrfb_base);
360 num_ctxs = pdev->num_resources - 1;
362 ctxs = devm_kzalloc(&pdev->dev,
363 sizeof(struct vrfb_ctx) * num_ctxs,
364 GFP_KERNEL);
366 if (!ctxs)
367 return -ENOMEM;
369 for (i = 0; i < num_ctxs; ++i) {
370 mem = platform_get_resource(pdev, IORESOURCE_MEM, 1 + i);
371 if (!mem) {
372 dev_err(&pdev->dev, "can't get vrfb ctx %d address\n",
374 return -EINVAL;
377 ctxs[i].base = mem->start;
380 vrfb_loaded = true;
382 return 0;
385 static void __exit vrfb_remove(struct platform_device *pdev)
387 vrfb_loaded = false;
390 static struct platform_driver vrfb_driver = {
391 .driver.name = "omapvrfb",
392 .remove = __exit_p(vrfb_remove),
395 module_platform_driver_probe(vrfb_driver, vrfb_probe);
397 MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@ti.com>");
398 MODULE_DESCRIPTION("OMAP VRFB");
399 MODULE_LICENSE("GPL v2");