video: Convert to devm_ioremap_resource()
[deliverable/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/err.h>
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/ioport.h>
27 #include <linux/io.h>
28 #include <linux/bitops.h>
29 #include <linux/mutex.h>
30 #include <linux/platform_device.h>
31
32 #include <video/omapvrfb.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_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))
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 /* bitmap of reserved contexts */
58 static unsigned long ctx_map;
59
60 struct vrfb_ctx {
61 u32 base;
62 u32 physical_ba;
63 u32 control;
64 u32 size;
65 };
66
67 static DEFINE_MUTEX(ctx_lock);
68
69 /*
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
73 * has finished.
74 */
75
76 static void __iomem *vrfb_base;
77
78 static int num_ctxs;
79 static struct vrfb_ctx *ctxs;
80
81 static bool vrfb_loaded;
82
83 static void omap2_sms_write_rot_control(u32 val, unsigned ctx)
84 {
85 __raw_writel(val, vrfb_base + SMS_ROT_CONTROL(ctx));
86 }
87
88 static void omap2_sms_write_rot_size(u32 val, unsigned ctx)
89 {
90 __raw_writel(val, vrfb_base + SMS_ROT_SIZE(ctx));
91 }
92
93 static void omap2_sms_write_rot_physical_ba(u32 val, unsigned ctx)
94 {
95 __raw_writel(val, vrfb_base + SMS_ROT_PHYSICAL_BA(ctx));
96 }
97
98 static inline void restore_hw_context(int ctx)
99 {
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);
103 }
104
105 static u32 get_image_width_roundup(u16 width, u8 bytespp)
106 {
107 unsigned long stride = width * bytespp;
108 unsigned long ceil_pages_per_stride = (stride / VRFB_PAGE_WIDTH) +
109 (stride % VRFB_PAGE_WIDTH != 0);
110
111 return ceil_pages_per_stride * VRFB_PAGE_WIDTH / bytespp;
112 }
113
114 /*
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
117 * area.
118 */
119 static inline u32 get_extra_physical_size(u16 image_width_roundup, u8 bytespp)
120 {
121 return (OMAP_VRFB_LINE_LEN - image_width_roundup) * VRFB_PAGE_HEIGHT *
122 bytespp;
123 }
124
125 void omap_vrfb_restore_context(void)
126 {
127 int i;
128 unsigned long map = ctx_map;
129
130 for (i = ffs(map); i; i = ffs(map)) {
131 /* i=1..32 */
132 i--;
133 map &= ~(1 << i);
134 restore_hw_context(i);
135 }
136 }
137
138 void omap_vrfb_adjust_size(u16 *width, u16 *height,
139 u8 bytespp)
140 {
141 *width = ALIGN(*width * bytespp, VRFB_PAGE_WIDTH) / bytespp;
142 *height = ALIGN(*height, VRFB_PAGE_HEIGHT);
143 }
144 EXPORT_SYMBOL(omap_vrfb_adjust_size);
145
146 u32 omap_vrfb_min_phys_size(u16 width, u16 height, u8 bytespp)
147 {
148 unsigned long image_width_roundup = get_image_width_roundup(width,
149 bytespp);
150
151 if (image_width_roundup > OMAP_VRFB_LINE_LEN)
152 return 0;
153
154 return (width * height * bytespp) + get_extra_physical_size(
155 image_width_roundup, bytespp);
156 }
157 EXPORT_SYMBOL(omap_vrfb_min_phys_size);
158
159 u16 omap_vrfb_max_height(u32 phys_size, u16 width, u8 bytespp)
160 {
161 unsigned long image_width_roundup = get_image_width_roundup(width,
162 bytespp);
163 unsigned long height;
164 unsigned long extra;
165
166 if (image_width_roundup > OMAP_VRFB_LINE_LEN)
167 return 0;
168
169 extra = get_extra_physical_size(image_width_roundup, bytespp);
170
171 if (phys_size < extra)
172 return 0;
173
174 height = (phys_size - extra) / (width * bytespp);
175
176 /* Virtual views provided by VRFB are limited to 2048x2048. */
177 return min_t(unsigned long, height, 2048);
178 }
179 EXPORT_SYMBOL(omap_vrfb_max_height);
180
181 void omap_vrfb_setup(struct vrfb *vrfb, unsigned long paddr,
182 u16 width, u16 height,
183 unsigned bytespp, bool yuv_mode)
184 {
185 unsigned pixel_size_exp;
186 u16 vrfb_width;
187 u16 vrfb_height;
188 u8 ctx = vrfb->context;
189 u32 size;
190 u32 control;
191
192 DBG("omapfb_set_vrfb(%d, %lx, %dx%d, %d, %d)\n", ctx, paddr,
193 width, height, bytespp, yuv_mode);
194
195 /* For YUV2 and UYVY modes VRFB needs to handle pixels a bit
196 * differently. See TRM. */
197 if (yuv_mode) {
198 bytespp *= 2;
199 width /= 2;
200 }
201
202 if (bytespp == 4)
203 pixel_size_exp = 2;
204 else if (bytespp == 2)
205 pixel_size_exp = 1;
206 else {
207 BUG();
208 return;
209 }
210
211 vrfb_width = ALIGN(width * bytespp, VRFB_PAGE_WIDTH) / bytespp;
212 vrfb_height = ALIGN(height, VRFB_PAGE_HEIGHT);
213
214 DBG("vrfb w %u, h %u bytespp %d\n", vrfb_width, vrfb_height, bytespp);
215
216 size = vrfb_width << SMS_IMAGEWIDTH_OFFSET;
217 size |= vrfb_height << SMS_IMAGEHEIGHT_OFFSET;
218
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;
222
223 ctxs[ctx].physical_ba = paddr;
224 ctxs[ctx].size = size;
225 ctxs[ctx].control = control;
226
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);
230
231 DBG("vrfb offset pixels %d, %d\n",
232 vrfb_width - width, vrfb_height - height);
233
234 vrfb->xres = width;
235 vrfb->yres = height;
236 vrfb->xoffset = vrfb_width - width;
237 vrfb->yoffset = vrfb_height - height;
238 vrfb->bytespp = bytespp;
239 vrfb->yuv_mode = yuv_mode;
240 }
241 EXPORT_SYMBOL(omap_vrfb_setup);
242
243 int omap_vrfb_map_angle(struct vrfb *vrfb, u16 height, u8 rot)
244 {
245 unsigned long size = height * OMAP_VRFB_LINE_LEN * vrfb->bytespp;
246
247 vrfb->vaddr[rot] = ioremap_wc(vrfb->paddr[rot], size);
248
249 if (!vrfb->vaddr[rot]) {
250 printk(KERN_ERR "vrfb: ioremap failed\n");
251 return -ENOMEM;
252 }
253
254 DBG("ioremapped vrfb area %d of size %lu into %p\n", rot, size,
255 vrfb->vaddr[rot]);
256
257 return 0;
258 }
259 EXPORT_SYMBOL(omap_vrfb_map_angle);
260
261 void omap_vrfb_release_ctx(struct vrfb *vrfb)
262 {
263 int rot;
264 int ctx = vrfb->context;
265
266 if (ctx == 0xff)
267 return;
268
269 DBG("release ctx %d\n", ctx);
270
271 mutex_lock(&ctx_lock);
272
273 BUG_ON(!(ctx_map & (1 << ctx)));
274
275 clear_bit(ctx, &ctx_map);
276
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;
281 }
282 }
283
284 vrfb->context = 0xff;
285
286 mutex_unlock(&ctx_lock);
287 }
288 EXPORT_SYMBOL(omap_vrfb_release_ctx);
289
290 int omap_vrfb_request_ctx(struct vrfb *vrfb)
291 {
292 int rot;
293 u32 paddr;
294 u8 ctx;
295 int r;
296
297 DBG("request ctx\n");
298
299 mutex_lock(&ctx_lock);
300
301 for (ctx = 0; ctx < num_ctxs; ++ctx)
302 if ((ctx_map & (1 << ctx)) == 0)
303 break;
304
305 if (ctx == num_ctxs) {
306 pr_err("vrfb: no free contexts\n");
307 r = -EBUSY;
308 goto out;
309 }
310
311 DBG("found free ctx %d\n", ctx);
312
313 set_bit(ctx, &ctx_map);
314
315 memset(vrfb, 0, sizeof(*vrfb));
316
317 vrfb->context = ctx;
318
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",
324 ctx, rot * 90);
325 omap_vrfb_release_ctx(vrfb);
326 r = -ENOMEM;
327 goto out;
328 }
329
330 vrfb->paddr[rot] = paddr;
331
332 DBG("VRFB %d/%d: %lx\n", ctx, rot*90, vrfb->paddr[rot]);
333 }
334
335 r = 0;
336 out:
337 mutex_unlock(&ctx_lock);
338 return r;
339 }
340 EXPORT_SYMBOL(omap_vrfb_request_ctx);
341
342 bool omap_vrfb_supported(void)
343 {
344 return vrfb_loaded;
345 }
346 EXPORT_SYMBOL(omap_vrfb_supported);
347
348 static int __init vrfb_probe(struct platform_device *pdev)
349 {
350 struct resource *mem;
351 int i;
352
353 /* first resource is the register res, the rest are vrfb contexts */
354
355 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
356 if (!mem) {
357 dev_err(&pdev->dev, "can't get vrfb base address\n");
358 return -EINVAL;
359 }
360
361 vrfb_base = devm_ioremap_resource(&pdev->dev, mem);
362 if (IS_ERR(vrfb_base))
363 return PTR_ERR(vrfb_base);
364
365 num_ctxs = pdev->num_resources - 1;
366
367 ctxs = devm_kzalloc(&pdev->dev,
368 sizeof(struct vrfb_ctx) * num_ctxs,
369 GFP_KERNEL);
370
371 if (!ctxs)
372 return -ENOMEM;
373
374 for (i = 0; i < num_ctxs; ++i) {
375 mem = platform_get_resource(pdev, IORESOURCE_MEM, 1 + i);
376 if (!mem) {
377 dev_err(&pdev->dev, "can't get vrfb ctx %d address\n",
378 i);
379 return -EINVAL;
380 }
381
382 ctxs[i].base = mem->start;
383 }
384
385 vrfb_loaded = true;
386
387 return 0;
388 }
389
390 static void __exit vrfb_remove(struct platform_device *pdev)
391 {
392 vrfb_loaded = false;
393 }
394
395 static struct platform_driver vrfb_driver = {
396 .driver.name = "omapvrfb",
397 .remove = __exit_p(vrfb_remove),
398 };
399
400 static int __init vrfb_init(void)
401 {
402 return platform_driver_probe(&vrfb_driver, &vrfb_probe);
403 }
404
405 static void __exit vrfb_exit(void)
406 {
407 platform_driver_unregister(&vrfb_driver);
408 }
409
410 module_init(vrfb_init);
411 module_exit(vrfb_exit);
412
413 MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@ti.com>");
414 MODULE_DESCRIPTION("OMAP VRFB");
415 MODULE_LICENSE("GPL v2");
This page took 0.042772 seconds and 5 git commands to generate.