]> rtime.felk.cvut.cz Git - can-eth-gw-linux.git/blob - drivers/video/omap2/vrfb.c
Merge tag 'davinci-for-v3.8/dt' of git://gitorious.org/linux-davinci/linux-davinci...
[can-eth-gw-linux.git] / drivers / video / omap2 / vrfb.c
1 /*
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>
29 #include <linux/platform_device.h>
30
31 #include <video/omapvrfb.h>
32
33 #ifdef DEBUG
34 #define DBG(format, ...) pr_debug("VRFB: " format, ## __VA_ARGS__)
35 #else
36 #define DBG(format, ...)
37 #endif
38
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))
43
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
56 /* bitmap of reserved contexts */
57 static unsigned long ctx_map;
58
59 struct vrfb_ctx {
60         u32 base;
61         u32 physical_ba;
62         u32 control;
63         u32 size;
64 };
65
66 static 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  */
74
75 static void __iomem *vrfb_base;
76
77 static int num_ctxs;
78 static struct vrfb_ctx *ctxs;
79
80 static bool vrfb_loaded;
81
82 static void omap2_sms_write_rot_control(u32 val, unsigned ctx)
83 {
84         __raw_writel(val, vrfb_base + SMS_ROT_CONTROL(ctx));
85 }
86
87 static void omap2_sms_write_rot_size(u32 val, unsigned ctx)
88 {
89         __raw_writel(val, vrfb_base + SMS_ROT_SIZE(ctx));
90 }
91
92 static void omap2_sms_write_rot_physical_ba(u32 val, unsigned ctx)
93 {
94         __raw_writel(val, vrfb_base + SMS_ROT_PHYSICAL_BA(ctx));
95 }
96
97 static inline void restore_hw_context(int ctx)
98 {
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);
102 }
103
104 static 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  */
118 static 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
124 void 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
137 void 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 }
143 EXPORT_SYMBOL(omap_vrfb_adjust_size);
144
145 u32 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 }
156 EXPORT_SYMBOL(omap_vrfb_min_phys_size);
157
158 u16 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 }
178 EXPORT_SYMBOL(omap_vrfb_max_height);
179
180 void 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;
205         else {
206                 BUG();
207                 return;
208         }
209
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
222         ctxs[ctx].physical_ba = paddr;
223         ctxs[ctx].size = size;
224         ctxs[ctx].control = control;
225
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 }
240 EXPORT_SYMBOL(omap_vrfb_setup);
241
242 int 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 }
258 EXPORT_SYMBOL(omap_vrfb_map_angle);
259
260 void 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 }
287 EXPORT_SYMBOL(omap_vrfb_release_ctx);
288
289 int 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
300         for (ctx = 0; ctx < num_ctxs; ++ctx)
301                 if ((ctx_map & (1 << ctx)) == 0)
302                         break;
303
304         if (ctx == num_ctxs) {
305                 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) {
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",
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;
335 out:
336         mutex_unlock(&ctx_lock);
337         return r;
338 }
339 EXPORT_SYMBOL(omap_vrfb_request_ctx);
340
341 bool omap_vrfb_supported(void)
342 {
343         return vrfb_loaded;
344 }
345 EXPORT_SYMBOL(omap_vrfb_supported);
346
347 static 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
386         vrfb_loaded = true;
387
388         return 0;
389 }
390
391 static void __exit vrfb_remove(struct platform_device *pdev)
392 {
393         vrfb_loaded = false;
394 }
395
396 static struct platform_driver vrfb_driver = {
397         .driver.name    = "omapvrfb",
398         .remove         = __exit_p(vrfb_remove),
399 };
400
401 static int __init vrfb_init(void)
402 {
403         return platform_driver_probe(&vrfb_driver, &vrfb_probe);
404 }
405
406 static void __exit vrfb_exit(void)
407 {
408         platform_driver_unregister(&vrfb_driver);
409 }
410
411 module_init(vrfb_init);
412 module_exit(vrfb_exit);
413
414 MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@ti.com>");
415 MODULE_DESCRIPTION("OMAP VRFB");
416 MODULE_LICENSE("GPL v2");