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.
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/ioport.h>
27 #include <linux/bitops.h>
28 #include <linux/mutex.h>
29 #include <linux/platform_device.h>
31 #include <video/omapvrfb.h>
34 #define DBG(format, ...) pr_debug("VRFB: " format, ## __VA_ARGS__)
36 #define DBG(format, ...)
39 #define SMS_ROT_CONTROL(context) (0x0 + 0x10 * context)
40 #define SMS_ROT_SIZE(context) (0x4 + 0x10 * context)
41 #define SMS_ROT_PHYSICAL_BA(context) (0x8 + 0x10 * context)
42 #define SMS_ROT_VIRT_BASE(rot) (0x1000000 * (rot))
44 #define OMAP_VRFB_SIZE (2048 * 2048 * 4)
46 #define VRFB_PAGE_WIDTH_EXP 5 /* Assuming SDRAM pagesize= 1024 */
47 #define VRFB_PAGE_HEIGHT_EXP 5 /* 1024 = 2^5 * 2^5 */
48 #define VRFB_PAGE_WIDTH (1 << VRFB_PAGE_WIDTH_EXP)
49 #define VRFB_PAGE_HEIGHT (1 << VRFB_PAGE_HEIGHT_EXP)
50 #define SMS_IMAGEHEIGHT_OFFSET 16
51 #define SMS_IMAGEWIDTH_OFFSET 0
52 #define SMS_PH_OFFSET 8
53 #define SMS_PW_OFFSET 4
54 #define SMS_PS_OFFSET 0
56 /* bitmap of reserved contexts */
57 static unsigned long ctx_map;
66 static DEFINE_MUTEX(ctx_lock);
69 * Access to this happens from client drivers or the PM core after wake-up.
70 * For the first case we require locking at the driver level, for the second
71 * we don't need locking, since no drivers will run until after the wake-up
75 static void __iomem *vrfb_base;
78 static struct vrfb_ctx *ctxs;
80 static bool vrfb_loaded;
82 static void omap2_sms_write_rot_control(u32 val, unsigned ctx)
84 __raw_writel(val, vrfb_base + SMS_ROT_CONTROL(ctx));
87 static void omap2_sms_write_rot_size(u32 val, unsigned ctx)
89 __raw_writel(val, vrfb_base + SMS_ROT_SIZE(ctx));
92 static void omap2_sms_write_rot_physical_ba(u32 val, unsigned ctx)
94 __raw_writel(val, vrfb_base + SMS_ROT_PHYSICAL_BA(ctx));
97 static inline void restore_hw_context(int ctx)
99 omap2_sms_write_rot_control(ctxs[ctx].control, ctx);
100 omap2_sms_write_rot_size(ctxs[ctx].size, ctx);
101 omap2_sms_write_rot_physical_ba(ctxs[ctx].physical_ba, ctx);
104 static u32 get_image_width_roundup(u16 width, u8 bytespp)
106 unsigned long stride = width * bytespp;
107 unsigned long ceil_pages_per_stride = (stride / VRFB_PAGE_WIDTH) +
108 (stride % VRFB_PAGE_WIDTH != 0);
110 return ceil_pages_per_stride * VRFB_PAGE_WIDTH / bytespp;
114 * This the extra space needed in the VRFB physical area for VRFB to safely wrap
115 * any memory accesses to the invisible part of the virtual view to the physical
118 static inline u32 get_extra_physical_size(u16 image_width_roundup, u8 bytespp)
120 return (OMAP_VRFB_LINE_LEN - image_width_roundup) * VRFB_PAGE_HEIGHT *
124 void omap_vrfb_restore_context(void)
127 unsigned long map = ctx_map;
129 for (i = ffs(map); i; i = ffs(map)) {
133 restore_hw_context(i);
137 void omap_vrfb_adjust_size(u16 *width, u16 *height,
140 *width = ALIGN(*width * bytespp, VRFB_PAGE_WIDTH) / bytespp;
141 *height = ALIGN(*height, VRFB_PAGE_HEIGHT);
143 EXPORT_SYMBOL(omap_vrfb_adjust_size);
145 u32 omap_vrfb_min_phys_size(u16 width, u16 height, u8 bytespp)
147 unsigned long image_width_roundup = get_image_width_roundup(width,
150 if (image_width_roundup > OMAP_VRFB_LINE_LEN)
153 return (width * height * bytespp) + get_extra_physical_size(
154 image_width_roundup, bytespp);
156 EXPORT_SYMBOL(omap_vrfb_min_phys_size);
158 u16 omap_vrfb_max_height(u32 phys_size, u16 width, u8 bytespp)
160 unsigned long image_width_roundup = get_image_width_roundup(width,
162 unsigned long height;
165 if (image_width_roundup > OMAP_VRFB_LINE_LEN)
168 extra = get_extra_physical_size(image_width_roundup, bytespp);
170 if (phys_size < extra)
173 height = (phys_size - extra) / (width * bytespp);
175 /* Virtual views provided by VRFB are limited to 2048x2048. */
176 return min_t(unsigned long, height, 2048);
178 EXPORT_SYMBOL(omap_vrfb_max_height);
180 void omap_vrfb_setup(struct vrfb *vrfb, unsigned long paddr,
181 u16 width, u16 height,
182 unsigned bytespp, bool yuv_mode)
184 unsigned pixel_size_exp;
187 u8 ctx = vrfb->context;
191 DBG("omapfb_set_vrfb(%d, %lx, %dx%d, %d, %d)\n", ctx, paddr,
192 width, height, bytespp, yuv_mode);
194 /* For YUV2 and UYVY modes VRFB needs to handle pixels a bit
195 * differently. See TRM. */
203 else if (bytespp == 2)
210 vrfb_width = ALIGN(width * bytespp, VRFB_PAGE_WIDTH) / bytespp;
211 vrfb_height = ALIGN(height, VRFB_PAGE_HEIGHT);
213 DBG("vrfb w %u, h %u bytespp %d\n", vrfb_width, vrfb_height, bytespp);
215 size = vrfb_width << SMS_IMAGEWIDTH_OFFSET;
216 size |= vrfb_height << SMS_IMAGEHEIGHT_OFFSET;
218 control = pixel_size_exp << SMS_PS_OFFSET;
219 control |= VRFB_PAGE_WIDTH_EXP << SMS_PW_OFFSET;
220 control |= VRFB_PAGE_HEIGHT_EXP << SMS_PH_OFFSET;
222 ctxs[ctx].physical_ba = paddr;
223 ctxs[ctx].size = size;
224 ctxs[ctx].control = control;
226 omap2_sms_write_rot_physical_ba(paddr, ctx);
227 omap2_sms_write_rot_size(size, ctx);
228 omap2_sms_write_rot_control(control, ctx);
230 DBG("vrfb offset pixels %d, %d\n",
231 vrfb_width - width, vrfb_height - height);
235 vrfb->xoffset = vrfb_width - width;
236 vrfb->yoffset = vrfb_height - height;
237 vrfb->bytespp = bytespp;
238 vrfb->yuv_mode = yuv_mode;
240 EXPORT_SYMBOL(omap_vrfb_setup);
242 int omap_vrfb_map_angle(struct vrfb *vrfb, u16 height, u8 rot)
244 unsigned long size = height * OMAP_VRFB_LINE_LEN * vrfb->bytespp;
246 vrfb->vaddr[rot] = ioremap_wc(vrfb->paddr[rot], size);
248 if (!vrfb->vaddr[rot]) {
249 printk(KERN_ERR "vrfb: ioremap failed\n");
253 DBG("ioremapped vrfb area %d of size %lu into %p\n", rot, size,
258 EXPORT_SYMBOL(omap_vrfb_map_angle);
260 void omap_vrfb_release_ctx(struct vrfb *vrfb)
263 int ctx = vrfb->context;
268 DBG("release ctx %d\n", ctx);
270 mutex_lock(&ctx_lock);
272 BUG_ON(!(ctx_map & (1 << ctx)));
274 clear_bit(ctx, &ctx_map);
276 for (rot = 0; rot < 4; ++rot) {
277 if (vrfb->paddr[rot]) {
278 release_mem_region(vrfb->paddr[rot], OMAP_VRFB_SIZE);
279 vrfb->paddr[rot] = 0;
283 vrfb->context = 0xff;
285 mutex_unlock(&ctx_lock);
287 EXPORT_SYMBOL(omap_vrfb_release_ctx);
289 int omap_vrfb_request_ctx(struct vrfb *vrfb)
296 DBG("request ctx\n");
298 mutex_lock(&ctx_lock);
300 for (ctx = 0; ctx < num_ctxs; ++ctx)
301 if ((ctx_map & (1 << ctx)) == 0)
304 if (ctx == num_ctxs) {
305 pr_err("vrfb: no free contexts\n");
310 DBG("found free ctx %d\n", ctx);
312 set_bit(ctx, &ctx_map);
314 memset(vrfb, 0, sizeof(*vrfb));
318 for (rot = 0; rot < 4; ++rot) {
319 paddr = ctxs[ctx].base + SMS_ROT_VIRT_BASE(rot);
320 if (!request_mem_region(paddr, OMAP_VRFB_SIZE, "vrfb")) {
321 pr_err("vrfb: failed to reserve VRFB "
322 "area for ctx %d, rotation %d\n",
324 omap_vrfb_release_ctx(vrfb);
329 vrfb->paddr[rot] = paddr;
331 DBG("VRFB %d/%d: %lx\n", ctx, rot*90, vrfb->paddr[rot]);
336 mutex_unlock(&ctx_lock);
339 EXPORT_SYMBOL(omap_vrfb_request_ctx);
341 bool omap_vrfb_supported(void)
345 EXPORT_SYMBOL(omap_vrfb_supported);
347 static int __init vrfb_probe(struct platform_device *pdev)
349 struct resource *mem;
352 /* first resource is the register res, the rest are vrfb contexts */
354 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
356 dev_err(&pdev->dev, "can't get vrfb base address\n");
360 vrfb_base = devm_request_and_ioremap(&pdev->dev, mem);
362 dev_err(&pdev->dev, "can't ioremap vrfb memory\n");
366 num_ctxs = pdev->num_resources - 1;
368 ctxs = devm_kzalloc(&pdev->dev,
369 sizeof(struct vrfb_ctx) * num_ctxs,
375 for (i = 0; i < num_ctxs; ++i) {
376 mem = platform_get_resource(pdev, IORESOURCE_MEM, 1 + i);
378 dev_err(&pdev->dev, "can't get vrfb ctx %d address\n",
383 ctxs[i].base = mem->start;
391 static void __exit vrfb_remove(struct platform_device *pdev)
396 static struct platform_driver vrfb_driver = {
397 .driver.name = "omapvrfb",
398 .remove = __exit_p(vrfb_remove),
401 static int __init vrfb_init(void)
403 return platform_driver_probe(&vrfb_driver, &vrfb_probe);
406 static void __exit vrfb_exit(void)
408 platform_driver_unregister(&vrfb_driver);
411 module_init(vrfb_init);
412 module_exit(vrfb_exit);
414 MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@ti.com>");
415 MODULE_DESCRIPTION("OMAP VRFB");
416 MODULE_LICENSE("GPL v2");