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/err.h>
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/ioport.h>
28 #include <linux/bitops.h>
29 #include <linux/mutex.h>
30 #include <linux/platform_device.h>
32 #include <video/omapvrfb.h>
35 #define DBG(format, ...) pr_debug("VRFB: " format, ## __VA_ARGS__)
37 #define DBG(format, ...)
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
;
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
76 static void __iomem
*vrfb_base
;
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
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
*
125 void omap_vrfb_restore_context(void)
128 unsigned long map
= ctx_map
;
130 for (i
= ffs(map
); i
; i
= ffs(map
)) {
134 restore_hw_context(i
);
138 void omap_vrfb_adjust_size(u16
*width
, u16
*height
,
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
,
151 if (image_width_roundup
> OMAP_VRFB_LINE_LEN
)
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
,
163 unsigned long height
;
166 if (image_width_roundup
> OMAP_VRFB_LINE_LEN
)
169 extra
= get_extra_physical_size(image_width_roundup
, bytespp
);
171 if (phys_size
< extra
)
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
;
188 u8 ctx
= vrfb
->context
;
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. */
204 else if (bytespp
== 2)
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
);
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");
254 DBG("ioremapped vrfb area %d of size %lu into %p\n", rot
, size
,
259 EXPORT_SYMBOL(omap_vrfb_map_angle
);
261 void omap_vrfb_release_ctx(struct vrfb
*vrfb
)
264 int ctx
= vrfb
->context
;
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
)
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)
305 if (ctx
== num_ctxs
) {
306 pr_err("vrfb: no free contexts\n");
311 DBG("found free ctx %d\n", ctx
);
313 set_bit(ctx
, &ctx_map
);
315 memset(vrfb
, 0, sizeof(*vrfb
));
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",
325 omap_vrfb_release_ctx(vrfb
);
330 vrfb
->paddr
[rot
] = paddr
;
332 DBG("VRFB %d/%d: %lx\n", ctx
, rot
*90, vrfb
->paddr
[rot
]);
337 mutex_unlock(&ctx_lock
);
340 EXPORT_SYMBOL(omap_vrfb_request_ctx
);
342 bool omap_vrfb_supported(void)
346 EXPORT_SYMBOL(omap_vrfb_supported
);
348 static int __init
vrfb_probe(struct platform_device
*pdev
)
350 struct resource
*mem
;
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
,
369 for (i
= 0; i
< num_ctxs
; ++i
) {
370 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1 + i
);
372 dev_err(&pdev
->dev
, "can't get vrfb ctx %d address\n",
377 ctxs
[i
].base
= mem
->start
;
385 static void __exit
vrfb_remove(struct platform_device
*pdev
)
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");