blob: f346b02eee1d84c2d8da77b15e3d64c72aa4fa17 [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
Thierry Redingbc3bad12013-01-21 11:09:23 +010023#include <linux/err.h>
Tomi Valkeinen640f9ca2009-08-07 12:04:26 +030024#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>
Tomi Valkeinen406c8562012-10-08 14:35:44 +030030#include <linux/platform_device.h>
Tomi Valkeinen640f9ca2009-08-07 12:04:26 +030031
Tomi Valkeinen6a1c9f62012-10-08 14:52:24 +030032#include <video/omapvrfb.h>
Tomi Valkeinen640f9ca2009-08-07 12:04:26 +030033
34#ifdef DEBUG
35#define DBG(format, ...) pr_debug("VRFB: " format, ## __VA_ARGS__)
36#else
37#define DBG(format, ...)
38#endif
39
Tomi Valkeinen406c8562012-10-08 14:35:44 +030040#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))
Tomi Valkeinen640f9ca2009-08-07 12:04:26 +030044
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
Tomi Valkeinen640f9ca2009-08-07 12:04:26 +030057/* bitmap of reserved contexts */
58static unsigned long ctx_map;
59
Tomi Valkeinen406c8562012-10-08 14:35:44 +030060struct vrfb_ctx {
61 u32 base;
62 u32 physical_ba;
63 u32 control;
64 u32 size;
65};
66
Tomi Valkeinen640f9ca2009-08-07 12:04:26 +030067static 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 */
Tomi Valkeinen406c8562012-10-08 14:35:44 +030075
76static void __iomem *vrfb_base;
77
78static int num_ctxs;
79static struct vrfb_ctx *ctxs;
80
Tomi Valkeinenaa1e49a2012-10-17 12:15:31 +030081static bool vrfb_loaded;
82
Tomi Valkeinen406c8562012-10-08 14:35:44 +030083static void omap2_sms_write_rot_control(u32 val, unsigned ctx)
84{
85 __raw_writel(val, vrfb_base + SMS_ROT_CONTROL(ctx));
86}
87
88static void omap2_sms_write_rot_size(u32 val, unsigned ctx)
89{
90 __raw_writel(val, vrfb_base + SMS_ROT_SIZE(ctx));
91}
92
93static void omap2_sms_write_rot_physical_ba(u32 val, unsigned ctx)
94{
95 __raw_writel(val, vrfb_base + SMS_ROT_PHYSICAL_BA(ctx));
96}
Tomi Valkeinen640f9ca2009-08-07 12:04:26 +030097
98static inline void restore_hw_context(int ctx)
99{
Tomi Valkeinen406c8562012-10-08 14:35:44 +0300100 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);
Tomi Valkeinen640f9ca2009-08-07 12:04:26 +0300103}
104
105static 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 */
119static 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
125void 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
138void 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}
144EXPORT_SYMBOL(omap_vrfb_adjust_size);
145
146u32 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}
157EXPORT_SYMBOL(omap_vrfb_min_phys_size);
158
159u16 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}
179EXPORT_SYMBOL(omap_vrfb_max_height);
180
181void 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;
Tomi Valkeinen3cb6a1b92012-05-18 11:49:53 +0300206 else {
Tomi Valkeinen640f9ca2009-08-07 12:04:26 +0300207 BUG();
Tomi Valkeinen3cb6a1b92012-05-18 11:49:53 +0300208 return;
209 }
Tomi Valkeinen640f9ca2009-08-07 12:04:26 +0300210
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
Tomi Valkeinen406c8562012-10-08 14:35:44 +0300223 ctxs[ctx].physical_ba = paddr;
224 ctxs[ctx].size = size;
225 ctxs[ctx].control = control;
Tomi Valkeinen640f9ca2009-08-07 12:04:26 +0300226
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}
241EXPORT_SYMBOL(omap_vrfb_setup);
242
243int 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}
259EXPORT_SYMBOL(omap_vrfb_map_angle);
260
261void 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}
288EXPORT_SYMBOL(omap_vrfb_release_ctx);
289
290int 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
Tomi Valkeinen406c8562012-10-08 14:35:44 +0300301 for (ctx = 0; ctx < num_ctxs; ++ctx)
Tomi Valkeinen640f9ca2009-08-07 12:04:26 +0300302 if ((ctx_map & (1 << ctx)) == 0)
303 break;
304
Tomi Valkeinen406c8562012-10-08 14:35:44 +0300305 if (ctx == num_ctxs) {
Tomi Valkeinen640f9ca2009-08-07 12:04:26 +0300306 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) {
Tomi Valkeinen406c8562012-10-08 14:35:44 +0300320 paddr = ctxs[ctx].base + SMS_ROT_VIRT_BASE(rot);
Tomi Valkeinen640f9ca2009-08-07 12:04:26 +0300321 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;
336out:
337 mutex_unlock(&ctx_lock);
338 return r;
339}
340EXPORT_SYMBOL(omap_vrfb_request_ctx);
Tomi Valkeinen406c8562012-10-08 14:35:44 +0300341
Tomi Valkeinenaa1e49a2012-10-17 12:15:31 +0300342bool omap_vrfb_supported(void)
343{
344 return vrfb_loaded;
345}
346EXPORT_SYMBOL(omap_vrfb_supported);
347
Tomi Valkeinen406c8562012-10-08 14:35:44 +0300348static 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);
Thierry Redingbc3bad12013-01-21 11:09:23 +0100356 vrfb_base = devm_ioremap_resource(&pdev->dev, mem);
357 if (IS_ERR(vrfb_base))
358 return PTR_ERR(vrfb_base);
Tomi Valkeinen406c8562012-10-08 14:35:44 +0300359
360 num_ctxs = pdev->num_resources - 1;
361
362 ctxs = devm_kzalloc(&pdev->dev,
363 sizeof(struct vrfb_ctx) * num_ctxs,
364 GFP_KERNEL);
365
366 if (!ctxs)
367 return -ENOMEM;
368
369 for (i = 0; i < num_ctxs; ++i) {
370 mem = platform_get_resource(pdev, IORESOURCE_MEM, 1 + i);
371 if (!mem) {
372 dev_err(&pdev->dev, "can't get vrfb ctx %d address\n",
373 i);
374 return -EINVAL;
375 }
376
377 ctxs[i].base = mem->start;
378 }
379
Tomi Valkeinenaa1e49a2012-10-17 12:15:31 +0300380 vrfb_loaded = true;
381
Tomi Valkeinen406c8562012-10-08 14:35:44 +0300382 return 0;
383}
384
Tomi Valkeinenaa1e49a2012-10-17 12:15:31 +0300385static void __exit vrfb_remove(struct platform_device *pdev)
386{
387 vrfb_loaded = false;
388}
389
Tomi Valkeinen406c8562012-10-08 14:35:44 +0300390static struct platform_driver vrfb_driver = {
391 .driver.name = "omapvrfb",
Tomi Valkeinenaa1e49a2012-10-17 12:15:31 +0300392 .remove = __exit_p(vrfb_remove),
Tomi Valkeinen406c8562012-10-08 14:35:44 +0300393};
394
Fabio Porcedda3ccbf892013-03-15 14:02:38 +0100395module_platform_driver_probe(vrfb_driver, vrfb_probe);
Tomi Valkeinen406c8562012-10-08 14:35:44 +0300396
397MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@ti.com>");
398MODULE_DESCRIPTION("OMAP VRFB");
399MODULE_LICENSE("GPL v2");