Merge branch 'devel' of master.kernel.org:/home/rmk/linux-2.6-arm
[safe/jmp/linux-2.6] / 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
30 #include <mach/io.h>
31 #include <plat/vrfb.h>
32 #include <plat/sdrc.h>
33
34 #ifdef DEBUG
35 #define DBG(format, ...) pr_debug("VRFB: " format, ## __VA_ARGS__)
36 #else
37 #define DBG(format, ...)
38 #endif
39
40 #define SMS_ROT_VIRT_BASE(context, rot) \
41         (((context >= 4) ? 0xD0000000 : 0x70000000) \
42          + (0x4000000 * (context)) \
43          + (0x1000000 * (rot)))
44
45 #define OMAP_VRFB_SIZE                  (2048 * 2048 * 4)
46
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
56
57 #define VRFB_NUM_CTXS 12
58 /* bitmap of reserved contexts */
59 static unsigned long ctx_map;
60
61 static DEFINE_MUTEX(ctx_lock);
62
63 /*
64  * Access to this happens from client drivers or the PM core after wake-up.
65  * For the first case we require locking at the driver level, for the second
66  * we don't need locking, since no drivers will run until after the wake-up
67  * has finished.
68  */
69 static struct {
70         u32 physical_ba;
71         u32 control;
72         u32 size;
73 } vrfb_hw_context[VRFB_NUM_CTXS];
74
75 static inline void restore_hw_context(int ctx)
76 {
77         omap2_sms_write_rot_control(vrfb_hw_context[ctx].control, ctx);
78         omap2_sms_write_rot_size(vrfb_hw_context[ctx].size, ctx);
79         omap2_sms_write_rot_physical_ba(vrfb_hw_context[ctx].physical_ba, ctx);
80 }
81
82 static u32 get_image_width_roundup(u16 width, u8 bytespp)
83 {
84         unsigned long stride = width * bytespp;
85         unsigned long ceil_pages_per_stride = (stride / VRFB_PAGE_WIDTH) +
86                 (stride % VRFB_PAGE_WIDTH != 0);
87
88         return ceil_pages_per_stride * VRFB_PAGE_WIDTH / bytespp;
89 }
90
91 /*
92  * This the extra space needed in the VRFB physical area for VRFB to safely wrap
93  * any memory accesses to the invisible part of the virtual view to the physical
94  * area.
95  */
96 static inline u32 get_extra_physical_size(u16 image_width_roundup, u8 bytespp)
97 {
98         return (OMAP_VRFB_LINE_LEN - image_width_roundup) * VRFB_PAGE_HEIGHT *
99                 bytespp;
100 }
101
102 void omap_vrfb_restore_context(void)
103 {
104         int i;
105         unsigned long map = ctx_map;
106
107         for (i = ffs(map); i; i = ffs(map)) {
108                 /* i=1..32 */
109                 i--;
110                 map &= ~(1 << i);
111                 restore_hw_context(i);
112         }
113 }
114
115 void omap_vrfb_adjust_size(u16 *width, u16 *height,
116                 u8 bytespp)
117 {
118         *width = ALIGN(*width * bytespp, VRFB_PAGE_WIDTH) / bytespp;
119         *height = ALIGN(*height, VRFB_PAGE_HEIGHT);
120 }
121 EXPORT_SYMBOL(omap_vrfb_adjust_size);
122
123 u32 omap_vrfb_min_phys_size(u16 width, u16 height, u8 bytespp)
124 {
125         unsigned long image_width_roundup = get_image_width_roundup(width,
126                 bytespp);
127
128         if (image_width_roundup > OMAP_VRFB_LINE_LEN)
129                 return 0;
130
131         return (width * height * bytespp) + get_extra_physical_size(
132                 image_width_roundup, bytespp);
133 }
134 EXPORT_SYMBOL(omap_vrfb_min_phys_size);
135
136 u16 omap_vrfb_max_height(u32 phys_size, u16 width, u8 bytespp)
137 {
138         unsigned long image_width_roundup = get_image_width_roundup(width,
139                 bytespp);
140         unsigned long height;
141         unsigned long extra;
142
143         if (image_width_roundup > OMAP_VRFB_LINE_LEN)
144                 return 0;
145
146         extra = get_extra_physical_size(image_width_roundup, bytespp);
147
148         if (phys_size < extra)
149                 return 0;
150
151         height = (phys_size - extra) / (width * bytespp);
152
153         /* Virtual views provided by VRFB are limited to 2048x2048. */
154         return min_t(unsigned long, height, 2048);
155 }
156 EXPORT_SYMBOL(omap_vrfb_max_height);
157
158 void omap_vrfb_setup(struct vrfb *vrfb, unsigned long paddr,
159                 u16 width, u16 height,
160                 unsigned bytespp, bool yuv_mode)
161 {
162         unsigned pixel_size_exp;
163         u16 vrfb_width;
164         u16 vrfb_height;
165         u8 ctx = vrfb->context;
166         u32 size;
167         u32 control;
168
169         DBG("omapfb_set_vrfb(%d, %lx, %dx%d, %d, %d)\n", ctx, paddr,
170                         width, height, bytespp, yuv_mode);
171
172         /* For YUV2 and UYVY modes VRFB needs to handle pixels a bit
173          * differently. See TRM. */
174         if (yuv_mode) {
175                 bytespp *= 2;
176                 width /= 2;
177         }
178
179         if (bytespp == 4)
180                 pixel_size_exp = 2;
181         else if (bytespp == 2)
182                 pixel_size_exp = 1;
183         else
184                 BUG();
185
186         vrfb_width = ALIGN(width * bytespp, VRFB_PAGE_WIDTH) / bytespp;
187         vrfb_height = ALIGN(height, VRFB_PAGE_HEIGHT);
188
189         DBG("vrfb w %u, h %u bytespp %d\n", vrfb_width, vrfb_height, bytespp);
190
191         size  = vrfb_width << SMS_IMAGEWIDTH_OFFSET;
192         size |= vrfb_height << SMS_IMAGEHEIGHT_OFFSET;
193
194         control  = pixel_size_exp << SMS_PS_OFFSET;
195         control |= VRFB_PAGE_WIDTH_EXP  << SMS_PW_OFFSET;
196         control |= VRFB_PAGE_HEIGHT_EXP << SMS_PH_OFFSET;
197
198         vrfb_hw_context[ctx].physical_ba = paddr;
199         vrfb_hw_context[ctx].size = size;
200         vrfb_hw_context[ctx].control = control;
201
202         omap2_sms_write_rot_physical_ba(paddr, ctx);
203         omap2_sms_write_rot_size(size, ctx);
204         omap2_sms_write_rot_control(control, ctx);
205
206         DBG("vrfb offset pixels %d, %d\n",
207                         vrfb_width - width, vrfb_height - height);
208
209         vrfb->xres = width;
210         vrfb->yres = height;
211         vrfb->xoffset = vrfb_width - width;
212         vrfb->yoffset = vrfb_height - height;
213         vrfb->bytespp = bytespp;
214         vrfb->yuv_mode = yuv_mode;
215 }
216 EXPORT_SYMBOL(omap_vrfb_setup);
217
218 int omap_vrfb_map_angle(struct vrfb *vrfb, u16 height, u8 rot)
219 {
220         unsigned long size = height * OMAP_VRFB_LINE_LEN * vrfb->bytespp;
221
222         vrfb->vaddr[rot] = ioremap_wc(vrfb->paddr[rot], size);
223
224         if (!vrfb->vaddr[rot]) {
225                 printk(KERN_ERR "vrfb: ioremap failed\n");
226                 return -ENOMEM;
227         }
228
229         DBG("ioremapped vrfb area %d of size %lu into %p\n", rot, size,
230                 vrfb->vaddr[rot]);
231
232         return 0;
233 }
234 EXPORT_SYMBOL(omap_vrfb_map_angle);
235
236 void omap_vrfb_release_ctx(struct vrfb *vrfb)
237 {
238         int rot;
239         int ctx = vrfb->context;
240
241         if (ctx == 0xff)
242                 return;
243
244         DBG("release ctx %d\n", ctx);
245
246         mutex_lock(&ctx_lock);
247
248         BUG_ON(!(ctx_map & (1 << ctx)));
249
250         clear_bit(ctx, &ctx_map);
251
252         for (rot = 0; rot < 4; ++rot) {
253                 if (vrfb->paddr[rot]) {
254                         release_mem_region(vrfb->paddr[rot], OMAP_VRFB_SIZE);
255                         vrfb->paddr[rot] = 0;
256                 }
257         }
258
259         vrfb->context = 0xff;
260
261         mutex_unlock(&ctx_lock);
262 }
263 EXPORT_SYMBOL(omap_vrfb_release_ctx);
264
265 int omap_vrfb_request_ctx(struct vrfb *vrfb)
266 {
267         int rot;
268         u32 paddr;
269         u8 ctx;
270         int r;
271
272         DBG("request ctx\n");
273
274         mutex_lock(&ctx_lock);
275
276         for (ctx = 0; ctx < VRFB_NUM_CTXS; ++ctx)
277                 if ((ctx_map & (1 << ctx)) == 0)
278                         break;
279
280         if (ctx == VRFB_NUM_CTXS) {
281                 pr_err("vrfb: no free contexts\n");
282                 r = -EBUSY;
283                 goto out;
284         }
285
286         DBG("found free ctx %d\n", ctx);
287
288         set_bit(ctx, &ctx_map);
289
290         memset(vrfb, 0, sizeof(*vrfb));
291
292         vrfb->context = ctx;
293
294         for (rot = 0; rot < 4; ++rot) {
295                 paddr = SMS_ROT_VIRT_BASE(ctx, rot);
296                 if (!request_mem_region(paddr, OMAP_VRFB_SIZE, "vrfb")) {
297                         pr_err("vrfb: failed to reserve VRFB "
298                                         "area for ctx %d, rotation %d\n",
299                                         ctx, rot * 90);
300                         omap_vrfb_release_ctx(vrfb);
301                         r = -ENOMEM;
302                         goto out;
303                 }
304
305                 vrfb->paddr[rot] = paddr;
306
307                 DBG("VRFB %d/%d: %lx\n", ctx, rot*90, vrfb->paddr[rot]);
308         }
309
310         r = 0;
311 out:
312         mutex_unlock(&ctx_lock);
313         return r;
314 }
315 EXPORT_SYMBOL(omap_vrfb_request_ctx);