blob: 5d8fdac3b8003982242d1f5f865492752ccac4bf [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 Valkeinen6a1c9f62012-10-08 14:52:24 +030031#include <video/omapvrfb.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
Tomi Valkeinenaa1e49a2012-10-17 12:15:31 +030080static bool vrfb_loaded;
81
Tomi Valkeinen406c8562012-10-08 14:35:44 +030082static void omap2_sms_write_rot_control(u32 val, unsigned ctx)
83{
84 __raw_writel(val, vrfb_base + SMS_ROT_CONTROL(ctx));
85}
86
87static void omap2_sms_write_rot_size(u32 val, unsigned ctx)
88{
89 __raw_writel(val, vrfb_base + SMS_ROT_SIZE(ctx));
90}
91
92static void omap2_sms_write_rot_physical_ba(u32 val, unsigned ctx)
93{
94 __raw_writel(val, vrfb_base + SMS_ROT_PHYSICAL_BA(ctx));
95}
Tomi Valkeinen640f9ca2009-08-07 12:04:26 +030096
97static inline void restore_hw_context(int ctx)
98{
Tomi Valkeinen406c8562012-10-08 14:35:44 +030099 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);
Tomi Valkeinen640f9ca2009-08-07 12:04:26 +0300102}
103
104static u32 get_image_width_roundup(u16 width, u8 bytespp)
105{
106 unsigned long stride = width * bytespp;
107 unsigned long ceil_pages_per_stride = (stride / VRFB_PAGE_WIDTH) +
108 (stride % VRFB_PAGE_WIDTH != 0);
109
110 return ceil_pages_per_stride * VRFB_PAGE_WIDTH / bytespp;
111}
112
113/*
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
116 * area.
117 */
118static inline u32 get_extra_physical_size(u16 image_width_roundup, u8 bytespp)
119{
120 return (OMAP_VRFB_LINE_LEN - image_width_roundup) * VRFB_PAGE_HEIGHT *
121 bytespp;
122}
123
124void omap_vrfb_restore_context(void)
125{
126 int i;
127 unsigned long map = ctx_map;
128
129 for (i = ffs(map); i; i = ffs(map)) {
130 /* i=1..32 */
131 i--;
132 map &= ~(1 << i);
133 restore_hw_context(i);
134 }
135}
136
137void omap_vrfb_adjust_size(u16 *width, u16 *height,
138 u8 bytespp)
139{
140 *width = ALIGN(*width * bytespp, VRFB_PAGE_WIDTH) / bytespp;
141 *height = ALIGN(*height, VRFB_PAGE_HEIGHT);
142}
143EXPORT_SYMBOL(omap_vrfb_adjust_size);
144
145u32 omap_vrfb_min_phys_size(u16 width, u16 height, u8 bytespp)
146{
147 unsigned long image_width_roundup = get_image_width_roundup(width,
148 bytespp);
149
150 if (image_width_roundup > OMAP_VRFB_LINE_LEN)
151 return 0;
152
153 return (width * height * bytespp) + get_extra_physical_size(
154 image_width_roundup, bytespp);
155}
156EXPORT_SYMBOL(omap_vrfb_min_phys_size);
157
158u16 omap_vrfb_max_height(u32 phys_size, u16 width, u8 bytespp)
159{
160 unsigned long image_width_roundup = get_image_width_roundup(width,
161 bytespp);
162 unsigned long height;
163 unsigned long extra;
164
165 if (image_width_roundup > OMAP_VRFB_LINE_LEN)
166 return 0;
167
168 extra = get_extra_physical_size(image_width_roundup, bytespp);
169
170 if (phys_size < extra)
171 return 0;
172
173 height = (phys_size - extra) / (width * bytespp);
174
175 /* Virtual views provided by VRFB are limited to 2048x2048. */
176 return min_t(unsigned long, height, 2048);
177}
178EXPORT_SYMBOL(omap_vrfb_max_height);
179
180void omap_vrfb_setup(struct vrfb *vrfb, unsigned long paddr,
181 u16 width, u16 height,
182 unsigned bytespp, bool yuv_mode)
183{
184 unsigned pixel_size_exp;
185 u16 vrfb_width;
186 u16 vrfb_height;
187 u8 ctx = vrfb->context;
188 u32 size;
189 u32 control;
190
191 DBG("omapfb_set_vrfb(%d, %lx, %dx%d, %d, %d)\n", ctx, paddr,
192 width, height, bytespp, yuv_mode);
193
194 /* For YUV2 and UYVY modes VRFB needs to handle pixels a bit
195 * differently. See TRM. */
196 if (yuv_mode) {
197 bytespp *= 2;
198 width /= 2;
199 }
200
201 if (bytespp == 4)
202 pixel_size_exp = 2;
203 else if (bytespp == 2)
204 pixel_size_exp = 1;
Tomi Valkeinen3cb6a1b92012-05-18 11:49:53 +0300205 else {
Tomi Valkeinen640f9ca2009-08-07 12:04:26 +0300206 BUG();
Tomi Valkeinen3cb6a1b92012-05-18 11:49:53 +0300207 return;
208 }
Tomi Valkeinen640f9ca2009-08-07 12:04:26 +0300209
210 vrfb_width = ALIGN(width * bytespp, VRFB_PAGE_WIDTH) / bytespp;
211 vrfb_height = ALIGN(height, VRFB_PAGE_HEIGHT);
212
213 DBG("vrfb w %u, h %u bytespp %d\n", vrfb_width, vrfb_height, bytespp);
214
215 size = vrfb_width << SMS_IMAGEWIDTH_OFFSET;
216 size |= vrfb_height << SMS_IMAGEHEIGHT_OFFSET;
217
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;
221
Tomi Valkeinen406c8562012-10-08 14:35:44 +0300222 ctxs[ctx].physical_ba = paddr;
223 ctxs[ctx].size = size;
224 ctxs[ctx].control = control;
Tomi Valkeinen640f9ca2009-08-07 12:04:26 +0300225
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);
229
230 DBG("vrfb offset pixels %d, %d\n",
231 vrfb_width - width, vrfb_height - height);
232
233 vrfb->xres = width;
234 vrfb->yres = height;
235 vrfb->xoffset = vrfb_width - width;
236 vrfb->yoffset = vrfb_height - height;
237 vrfb->bytespp = bytespp;
238 vrfb->yuv_mode = yuv_mode;
239}
240EXPORT_SYMBOL(omap_vrfb_setup);
241
242int omap_vrfb_map_angle(struct vrfb *vrfb, u16 height, u8 rot)
243{
244 unsigned long size = height * OMAP_VRFB_LINE_LEN * vrfb->bytespp;
245
246 vrfb->vaddr[rot] = ioremap_wc(vrfb->paddr[rot], size);
247
248 if (!vrfb->vaddr[rot]) {
249 printk(KERN_ERR "vrfb: ioremap failed\n");
250 return -ENOMEM;
251 }
252
253 DBG("ioremapped vrfb area %d of size %lu into %p\n", rot, size,
254 vrfb->vaddr[rot]);
255
256 return 0;
257}
258EXPORT_SYMBOL(omap_vrfb_map_angle);
259
260void omap_vrfb_release_ctx(struct vrfb *vrfb)
261{
262 int rot;
263 int ctx = vrfb->context;
264
265 if (ctx == 0xff)
266 return;
267
268 DBG("release ctx %d\n", ctx);
269
270 mutex_lock(&ctx_lock);
271
272 BUG_ON(!(ctx_map & (1 << ctx)));
273
274 clear_bit(ctx, &ctx_map);
275
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;
280 }
281 }
282
283 vrfb->context = 0xff;
284
285 mutex_unlock(&ctx_lock);
286}
287EXPORT_SYMBOL(omap_vrfb_release_ctx);
288
289int omap_vrfb_request_ctx(struct vrfb *vrfb)
290{
291 int rot;
292 u32 paddr;
293 u8 ctx;
294 int r;
295
296 DBG("request ctx\n");
297
298 mutex_lock(&ctx_lock);
299
Tomi Valkeinen406c8562012-10-08 14:35:44 +0300300 for (ctx = 0; ctx < num_ctxs; ++ctx)
Tomi Valkeinen640f9ca2009-08-07 12:04:26 +0300301 if ((ctx_map & (1 << ctx)) == 0)
302 break;
303
Tomi Valkeinen406c8562012-10-08 14:35:44 +0300304 if (ctx == num_ctxs) {
Tomi Valkeinen640f9ca2009-08-07 12:04:26 +0300305 pr_err("vrfb: no free contexts\n");
306 r = -EBUSY;
307 goto out;
308 }
309
310 DBG("found free ctx %d\n", ctx);
311
312 set_bit(ctx, &ctx_map);
313
314 memset(vrfb, 0, sizeof(*vrfb));
315
316 vrfb->context = ctx;
317
318 for (rot = 0; rot < 4; ++rot) {
Tomi Valkeinen406c8562012-10-08 14:35:44 +0300319 paddr = ctxs[ctx].base + SMS_ROT_VIRT_BASE(rot);
Tomi Valkeinen640f9ca2009-08-07 12:04:26 +0300320 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",
323 ctx, rot * 90);
324 omap_vrfb_release_ctx(vrfb);
325 r = -ENOMEM;
326 goto out;
327 }
328
329 vrfb->paddr[rot] = paddr;
330
331 DBG("VRFB %d/%d: %lx\n", ctx, rot*90, vrfb->paddr[rot]);
332 }
333
334 r = 0;
335out:
336 mutex_unlock(&ctx_lock);
337 return r;
338}
339EXPORT_SYMBOL(omap_vrfb_request_ctx);
Tomi Valkeinen406c8562012-10-08 14:35:44 +0300340
Tomi Valkeinenaa1e49a2012-10-17 12:15:31 +0300341bool omap_vrfb_supported(void)
342{
343 return vrfb_loaded;
344}
345EXPORT_SYMBOL(omap_vrfb_supported);
346
Tomi Valkeinen406c8562012-10-08 14:35:44 +0300347static int __init vrfb_probe(struct platform_device *pdev)
348{
349 struct resource *mem;
350 int i;
351
352 /* first resource is the register res, the rest are vrfb contexts */
353
354 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
355 if (!mem) {
356 dev_err(&pdev->dev, "can't get vrfb base address\n");
357 return -EINVAL;
358 }
359
360 vrfb_base = devm_request_and_ioremap(&pdev->dev, mem);
361 if (!vrfb_base) {
362 dev_err(&pdev->dev, "can't ioremap vrfb memory\n");
363 return -ENOMEM;
364 }
365
366 num_ctxs = pdev->num_resources - 1;
367
368 ctxs = devm_kzalloc(&pdev->dev,
369 sizeof(struct vrfb_ctx) * num_ctxs,
370 GFP_KERNEL);
371
372 if (!ctxs)
373 return -ENOMEM;
374
375 for (i = 0; i < num_ctxs; ++i) {
376 mem = platform_get_resource(pdev, IORESOURCE_MEM, 1 + i);
377 if (!mem) {
378 dev_err(&pdev->dev, "can't get vrfb ctx %d address\n",
379 i);
380 return -EINVAL;
381 }
382
383 ctxs[i].base = mem->start;
384 }
385
Tomi Valkeinenaa1e49a2012-10-17 12:15:31 +0300386 vrfb_loaded = true;
387
Tomi Valkeinen406c8562012-10-08 14:35:44 +0300388 return 0;
389}
390
Tomi Valkeinenaa1e49a2012-10-17 12:15:31 +0300391static void __exit vrfb_remove(struct platform_device *pdev)
392{
393 vrfb_loaded = false;
394}
395
Tomi Valkeinen406c8562012-10-08 14:35:44 +0300396static struct platform_driver vrfb_driver = {
397 .driver.name = "omapvrfb",
Tomi Valkeinenaa1e49a2012-10-17 12:15:31 +0300398 .remove = __exit_p(vrfb_remove),
Tomi Valkeinen406c8562012-10-08 14:35:44 +0300399};
400
401static int __init vrfb_init(void)
402{
403 return platform_driver_probe(&vrfb_driver, &vrfb_probe);
404}
405
406static void __exit vrfb_exit(void)
407{
408 platform_driver_unregister(&vrfb_driver);
409}
410
411module_init(vrfb_init);
412module_exit(vrfb_exit);
413
414MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@ti.com>");
415MODULE_DESCRIPTION("OMAP VRFB");
416MODULE_LICENSE("GPL v2");