blob: fda45cc10d482b2beb1e813767b5ddbd389b165e [file] [log] [blame]
Tomi Valkeinen640f9ca2009-08-07 12:04:26 +03001/*
2 * VRFB Rotation Engine
3 *
4 * Copyright (C) 2009 Nokia Corporation
5 * Author: Tomi Valkeinen <tomi.valkeinen@nokia.com>
6 *
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.
10 *
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.
15 *
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.
19 */
20
21/*#define DEBUG*/
22
23#include <linux/kernel.h>
24#include <linux/module.h>
25#include <linux/ioport.h>
26#include <linux/io.h>
27#include <linux/bitops.h>
28#include <linux/mutex.h>
Tomi Valkeinen406c8562012-10-08 14:35:44 +030029#include <linux/platform_device.h>
Tomi Valkeinen640f9ca2009-08-07 12:04:26 +030030
Tomi Valkeinen640f9ca2009-08-07 12:04:26 +030031#include <plat/vrfb.h>
Tomi Valkeinen640f9ca2009-08-07 12:04:26 +030032
33#ifdef DEBUG
34#define DBG(format, ...) pr_debug("VRFB: " format, ## __VA_ARGS__)
35#else
36#define DBG(format, ...)
37#endif
38
Tomi Valkeinen406c8562012-10-08 14:35:44 +030039#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))
Tomi Valkeinen640f9ca2009-08-07 12:04:26 +030043
44#define OMAP_VRFB_SIZE (2048 * 2048 * 4)
45
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
55
Tomi Valkeinen640f9ca2009-08-07 12:04:26 +030056/* bitmap of reserved contexts */
57static unsigned long ctx_map;
58
Tomi Valkeinen406c8562012-10-08 14:35:44 +030059struct vrfb_ctx {
60 u32 base;
61 u32 physical_ba;
62 u32 control;
63 u32 size;
64};
65
Tomi Valkeinen640f9ca2009-08-07 12:04:26 +030066static DEFINE_MUTEX(ctx_lock);
67
68/*
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
72 * has finished.
73 */
Tomi Valkeinen406c8562012-10-08 14:35:44 +030074
75static void __iomem *vrfb_base;
76
77static int num_ctxs;
78static struct vrfb_ctx *ctxs;
79
80static void omap2_sms_write_rot_control(u32 val, unsigned ctx)
81{
82 __raw_writel(val, vrfb_base + SMS_ROT_CONTROL(ctx));
83}
84
85static void omap2_sms_write_rot_size(u32 val, unsigned ctx)
86{
87 __raw_writel(val, vrfb_base + SMS_ROT_SIZE(ctx));
88}
89
90static void omap2_sms_write_rot_physical_ba(u32 val, unsigned ctx)
91{
92 __raw_writel(val, vrfb_base + SMS_ROT_PHYSICAL_BA(ctx));
93}
Tomi Valkeinen640f9ca2009-08-07 12:04:26 +030094
95static inline void restore_hw_context(int ctx)
96{
Tomi Valkeinen406c8562012-10-08 14:35:44 +030097 omap2_sms_write_rot_control(ctxs[ctx].control, ctx);
98 omap2_sms_write_rot_size(ctxs[ctx].size, ctx);
99 omap2_sms_write_rot_physical_ba(ctxs[ctx].physical_ba, ctx);
Tomi Valkeinen640f9ca2009-08-07 12:04:26 +0300100}
101
102static u32 get_image_width_roundup(u16 width, u8 bytespp)
103{
104 unsigned long stride = width * bytespp;
105 unsigned long ceil_pages_per_stride = (stride / VRFB_PAGE_WIDTH) +
106 (stride % VRFB_PAGE_WIDTH != 0);
107
108 return ceil_pages_per_stride * VRFB_PAGE_WIDTH / bytespp;
109}
110
111/*
112 * This the extra space needed in the VRFB physical area for VRFB to safely wrap
113 * any memory accesses to the invisible part of the virtual view to the physical
114 * area.
115 */
116static inline u32 get_extra_physical_size(u16 image_width_roundup, u8 bytespp)
117{
118 return (OMAP_VRFB_LINE_LEN - image_width_roundup) * VRFB_PAGE_HEIGHT *
119 bytespp;
120}
121
122void omap_vrfb_restore_context(void)
123{
124 int i;
125 unsigned long map = ctx_map;
126
127 for (i = ffs(map); i; i = ffs(map)) {
128 /* i=1..32 */
129 i--;
130 map &= ~(1 << i);
131 restore_hw_context(i);
132 }
133}
134
135void omap_vrfb_adjust_size(u16 *width, u16 *height,
136 u8 bytespp)
137{
138 *width = ALIGN(*width * bytespp, VRFB_PAGE_WIDTH) / bytespp;
139 *height = ALIGN(*height, VRFB_PAGE_HEIGHT);
140}
141EXPORT_SYMBOL(omap_vrfb_adjust_size);
142
143u32 omap_vrfb_min_phys_size(u16 width, u16 height, u8 bytespp)
144{
145 unsigned long image_width_roundup = get_image_width_roundup(width,
146 bytespp);
147
148 if (image_width_roundup > OMAP_VRFB_LINE_LEN)
149 return 0;
150
151 return (width * height * bytespp) + get_extra_physical_size(
152 image_width_roundup, bytespp);
153}
154EXPORT_SYMBOL(omap_vrfb_min_phys_size);
155
156u16 omap_vrfb_max_height(u32 phys_size, u16 width, u8 bytespp)
157{
158 unsigned long image_width_roundup = get_image_width_roundup(width,
159 bytespp);
160 unsigned long height;
161 unsigned long extra;
162
163 if (image_width_roundup > OMAP_VRFB_LINE_LEN)
164 return 0;
165
166 extra = get_extra_physical_size(image_width_roundup, bytespp);
167
168 if (phys_size < extra)
169 return 0;
170
171 height = (phys_size - extra) / (width * bytespp);
172
173 /* Virtual views provided by VRFB are limited to 2048x2048. */
174 return min_t(unsigned long, height, 2048);
175}
176EXPORT_SYMBOL(omap_vrfb_max_height);
177
178void omap_vrfb_setup(struct vrfb *vrfb, unsigned long paddr,
179 u16 width, u16 height,
180 unsigned bytespp, bool yuv_mode)
181{
182 unsigned pixel_size_exp;
183 u16 vrfb_width;
184 u16 vrfb_height;
185 u8 ctx = vrfb->context;
186 u32 size;
187 u32 control;
188
189 DBG("omapfb_set_vrfb(%d, %lx, %dx%d, %d, %d)\n", ctx, paddr,
190 width, height, bytespp, yuv_mode);
191
192 /* For YUV2 and UYVY modes VRFB needs to handle pixels a bit
193 * differently. See TRM. */
194 if (yuv_mode) {
195 bytespp *= 2;
196 width /= 2;
197 }
198
199 if (bytespp == 4)
200 pixel_size_exp = 2;
201 else if (bytespp == 2)
202 pixel_size_exp = 1;
Tomi Valkeinen3cb6a1b2012-05-18 11:49:53 +0300203 else {
Tomi Valkeinen640f9ca2009-08-07 12:04:26 +0300204 BUG();
Tomi Valkeinen3cb6a1b2012-05-18 11:49:53 +0300205 return;
206 }
Tomi Valkeinen640f9ca2009-08-07 12:04:26 +0300207
208 vrfb_width = ALIGN(width * bytespp, VRFB_PAGE_WIDTH) / bytespp;
209 vrfb_height = ALIGN(height, VRFB_PAGE_HEIGHT);
210
211 DBG("vrfb w %u, h %u bytespp %d\n", vrfb_width, vrfb_height, bytespp);
212
213 size = vrfb_width << SMS_IMAGEWIDTH_OFFSET;
214 size |= vrfb_height << SMS_IMAGEHEIGHT_OFFSET;
215
216 control = pixel_size_exp << SMS_PS_OFFSET;
217 control |= VRFB_PAGE_WIDTH_EXP << SMS_PW_OFFSET;
218 control |= VRFB_PAGE_HEIGHT_EXP << SMS_PH_OFFSET;
219
Tomi Valkeinen406c8562012-10-08 14:35:44 +0300220 ctxs[ctx].physical_ba = paddr;
221 ctxs[ctx].size = size;
222 ctxs[ctx].control = control;
Tomi Valkeinen640f9ca2009-08-07 12:04:26 +0300223
224 omap2_sms_write_rot_physical_ba(paddr, ctx);
225 omap2_sms_write_rot_size(size, ctx);
226 omap2_sms_write_rot_control(control, ctx);
227
228 DBG("vrfb offset pixels %d, %d\n",
229 vrfb_width - width, vrfb_height - height);
230
231 vrfb->xres = width;
232 vrfb->yres = height;
233 vrfb->xoffset = vrfb_width - width;
234 vrfb->yoffset = vrfb_height - height;
235 vrfb->bytespp = bytespp;
236 vrfb->yuv_mode = yuv_mode;
237}
238EXPORT_SYMBOL(omap_vrfb_setup);
239
240int omap_vrfb_map_angle(struct vrfb *vrfb, u16 height, u8 rot)
241{
242 unsigned long size = height * OMAP_VRFB_LINE_LEN * vrfb->bytespp;
243
244 vrfb->vaddr[rot] = ioremap_wc(vrfb->paddr[rot], size);
245
246 if (!vrfb->vaddr[rot]) {
247 printk(KERN_ERR "vrfb: ioremap failed\n");
248 return -ENOMEM;
249 }
250
251 DBG("ioremapped vrfb area %d of size %lu into %p\n", rot, size,
252 vrfb->vaddr[rot]);
253
254 return 0;
255}
256EXPORT_SYMBOL(omap_vrfb_map_angle);
257
258void omap_vrfb_release_ctx(struct vrfb *vrfb)
259{
260 int rot;
261 int ctx = vrfb->context;
262
263 if (ctx == 0xff)
264 return;
265
266 DBG("release ctx %d\n", ctx);
267
268 mutex_lock(&ctx_lock);
269
270 BUG_ON(!(ctx_map & (1 << ctx)));
271
272 clear_bit(ctx, &ctx_map);
273
274 for (rot = 0; rot < 4; ++rot) {
275 if (vrfb->paddr[rot]) {
276 release_mem_region(vrfb->paddr[rot], OMAP_VRFB_SIZE);
277 vrfb->paddr[rot] = 0;
278 }
279 }
280
281 vrfb->context = 0xff;
282
283 mutex_unlock(&ctx_lock);
284}
285EXPORT_SYMBOL(omap_vrfb_release_ctx);
286
287int omap_vrfb_request_ctx(struct vrfb *vrfb)
288{
289 int rot;
290 u32 paddr;
291 u8 ctx;
292 int r;
293
294 DBG("request ctx\n");
295
296 mutex_lock(&ctx_lock);
297
Tomi Valkeinen406c8562012-10-08 14:35:44 +0300298 for (ctx = 0; ctx < num_ctxs; ++ctx)
Tomi Valkeinen640f9ca2009-08-07 12:04:26 +0300299 if ((ctx_map & (1 << ctx)) == 0)
300 break;
301
Tomi Valkeinen406c8562012-10-08 14:35:44 +0300302 if (ctx == num_ctxs) {
Tomi Valkeinen640f9ca2009-08-07 12:04:26 +0300303 pr_err("vrfb: no free contexts\n");
304 r = -EBUSY;
305 goto out;
306 }
307
308 DBG("found free ctx %d\n", ctx);
309
310 set_bit(ctx, &ctx_map);
311
312 memset(vrfb, 0, sizeof(*vrfb));
313
314 vrfb->context = ctx;
315
316 for (rot = 0; rot < 4; ++rot) {
Tomi Valkeinen406c8562012-10-08 14:35:44 +0300317 paddr = ctxs[ctx].base + SMS_ROT_VIRT_BASE(rot);
Tomi Valkeinen640f9ca2009-08-07 12:04:26 +0300318 if (!request_mem_region(paddr, OMAP_VRFB_SIZE, "vrfb")) {
319 pr_err("vrfb: failed to reserve VRFB "
320 "area for ctx %d, rotation %d\n",
321 ctx, rot * 90);
322 omap_vrfb_release_ctx(vrfb);
323 r = -ENOMEM;
324 goto out;
325 }
326
327 vrfb->paddr[rot] = paddr;
328
329 DBG("VRFB %d/%d: %lx\n", ctx, rot*90, vrfb->paddr[rot]);
330 }
331
332 r = 0;
333out:
334 mutex_unlock(&ctx_lock);
335 return r;
336}
337EXPORT_SYMBOL(omap_vrfb_request_ctx);
Tomi Valkeinen406c8562012-10-08 14:35:44 +0300338
339static int __init vrfb_probe(struct platform_device *pdev)
340{
341 struct resource *mem;
342 int i;
343
344 /* first resource is the register res, the rest are vrfb contexts */
345
346 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
347 if (!mem) {
348 dev_err(&pdev->dev, "can't get vrfb base address\n");
349 return -EINVAL;
350 }
351
352 vrfb_base = devm_request_and_ioremap(&pdev->dev, mem);
353 if (!vrfb_base) {
354 dev_err(&pdev->dev, "can't ioremap vrfb memory\n");
355 return -ENOMEM;
356 }
357
358 num_ctxs = pdev->num_resources - 1;
359
360 ctxs = devm_kzalloc(&pdev->dev,
361 sizeof(struct vrfb_ctx) * num_ctxs,
362 GFP_KERNEL);
363
364 if (!ctxs)
365 return -ENOMEM;
366
367 for (i = 0; i < num_ctxs; ++i) {
368 mem = platform_get_resource(pdev, IORESOURCE_MEM, 1 + i);
369 if (!mem) {
370 dev_err(&pdev->dev, "can't get vrfb ctx %d address\n",
371 i);
372 return -EINVAL;
373 }
374
375 ctxs[i].base = mem->start;
376 }
377
378 return 0;
379}
380
381static struct platform_driver vrfb_driver = {
382 .driver.name = "omapvrfb",
383};
384
385static int __init vrfb_init(void)
386{
387 return platform_driver_probe(&vrfb_driver, &vrfb_probe);
388}
389
390static void __exit vrfb_exit(void)
391{
392 platform_driver_unregister(&vrfb_driver);
393}
394
395module_init(vrfb_init);
396module_exit(vrfb_exit);
397
398MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@ti.com>");
399MODULE_DESCRIPTION("OMAP VRFB");
400MODULE_LICENSE("GPL v2");