summaryrefslogtreecommitdiffstats
path: root/vidix/sh_veu_vid.c
blob: cc45e32d861558f050cefb655ebbbacb1cc7b48d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
/*
 * VIDIX driver for SuperH Mobile VEU hardware block.
 * Copyright (C) 2008, 2009 Magnus Damm
 *
 * Requires a kernel that exposes the VEU hardware block to user space
 * using UIO. Available in upstream linux-2.6.27 or later.
 *
 * Tested using WVGA and QVGA panels with sh7722 VEU and sh7723 VEU2H.
 *
 * This file is part of MPlayer.
 *
 * MPlayer is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * MPlayer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with MPlayer; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <linux/fb.h>
#include <inttypes.h>
#include <unistd.h>
#include <errno.h>

#include "config.h"
#include "vidix.h"
#include "fourcc.h"

#include "dha.h"

static int fgets_with_openclose(char *fname, char *buf, size_t maxlen)
{
    FILE *fp;

    fp = fopen(fname, "r");
    if (!fp)
        return -1;

    fgets(buf, maxlen, fp);
    fclose(fp);
    return strlen(buf);
}

struct uio_device {
    char *name;
    char *path;
    int fd;
};

#define MAXNAMELEN 256

static int locate_uio_device(char *name, struct uio_device *udp)
{
    char fname[MAXNAMELEN], buf[MAXNAMELEN];
    char *tmp;
    int uio_id;

    uio_id = -1;
    do {
        uio_id++;
        snprintf(fname, MAXNAMELEN, "/sys/class/uio/uio%d/name", uio_id);
        if (fgets_with_openclose(fname, buf, MAXNAMELEN) < 0)
            return -1;

    } while (strncmp(name, buf, strlen(name)));

    tmp = strchr(buf, '\n');
    if (tmp)
        *tmp = '\0';

    udp->name = strdup(buf);
    udp->path = strdup(fname);
    udp->path[strlen(udp->path) - 5] = '\0';

    snprintf(buf, MAXNAMELEN, "/dev/uio%d", uio_id);
    udp->fd = open(buf, O_RDWR | O_SYNC);

    if (udp->fd < 0)
        return -1;

    return 0;
}

struct uio_map {
    unsigned long address;
    unsigned long size;
    void *iomem;
};

static int setup_uio_map(struct uio_device *udp, int nr, struct uio_map *ump)
{
    char fname[MAXNAMELEN], buf[MAXNAMELEN];

    snprintf(fname, MAXNAMELEN, "%s/maps/map%d/addr", udp->path, nr);
    if (fgets_with_openclose(fname, buf, MAXNAMELEN) <= 0)
        return -1;

    ump->address = strtoul(buf, NULL, 0);

    snprintf(fname, MAXNAMELEN, "%s/maps/map%d/size", udp->path, nr);
    if (fgets_with_openclose(fname, buf, MAXNAMELEN) <= 0)
        return -1;

    ump->size = strtoul(buf, NULL, 0);
    ump->iomem = mmap(0, ump->size,
                      PROT_READ|PROT_WRITE, MAP_SHARED,
                      udp->fd, nr * getpagesize());

    if (ump->iomem == MAP_FAILED)
        return -1;

    return 0;
}

struct fb_info {
    unsigned long width;
    unsigned long height;
    unsigned long bpp;
    unsigned long line_length;

    unsigned long address;
    unsigned long size;
};

static int get_fb_info(char *device, struct fb_info *fip)
{
    struct fb_var_screeninfo vinfo;
    struct fb_fix_screeninfo finfo;
    void *iomem;
    int fd;

    fd = open(device, O_RDWR);
    if (fd < 0) {
        perror("open");
        return -1;
    }

    if (ioctl(fd, FBIOGET_VSCREENINFO, &vinfo) == -1) {
        perror("ioctl(FBIOGET_VSCREENINFO)");
        return -1;
    }

    fip->width = vinfo.xres;
    fip->height = vinfo.yres;
    fip->bpp = vinfo.bits_per_pixel;

    if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1) {
        perror("ioctl(FBIOGET_FSCREENINFO)");
        return -1;
    }

    fip->address = finfo.smem_start;
    fip->size = finfo.smem_len;
    fip->line_length = finfo.line_length;

    iomem = mmap(0, fip->size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);

    if (iomem == MAP_FAILED) {
        perror("mmap");
        return -1;
    }

    /* clear framebuffer */
    memset(iomem, 0, fip->line_length * fip->height);
    munmap(iomem, fip->size);

    return fd;
}

#define VESTR 0x00 /* start register */
#define VESWR 0x10 /* src: line length */
#define VESSR 0x14 /* src: image size */
#define VSAYR 0x18 /* src: y/rgb plane address */
#define VSACR 0x1c /* src: c plane address */
#define VBSSR 0x20 /* bundle mode register */
#define VEDWR 0x30 /* dst: line length */
#define VDAYR 0x34 /* dst: y/rgb plane address */
#define VDACR 0x38 /* dst: c plane address */
#define VTRCR 0x50 /* transform control */
#define VRFCR 0x54 /* resize scale */
#define VRFSR 0x58 /* resize clip */
#define VENHR 0x5c /* enhance */
#define VFMCR 0x70 /* filter mode */
#define VVTCR 0x74 /* lowpass vertical */
#define VHTCR 0x78 /* lowpass horizontal */
#define VAPCR 0x80 /* color match */
#define VECCR 0x84 /* color replace */
#define VAFXR 0x90 /* fixed mode */
#define VSWPR 0x94 /* swap */
#define VEIER 0xa0 /* interrupt mask */
#define VEVTR 0xa4 /* interrupt event */
#define VSTAR 0xb0 /* status */
#define VBSRR 0xb4 /* reset */

#define VMCR00 0x200 /* color conversion matrix coefficient 00 */
#define VMCR01 0x204 /* color conversion matrix coefficient 01 */
#define VMCR02 0x208 /* color conversion matrix coefficient 02 */
#define VMCR10 0x20c /* color conversion matrix coefficient 10 */
#define VMCR11 0x210 /* color conversion matrix coefficient 11 */
#define VMCR12 0x214 /* color conversion matrix coefficient 12 */
#define VMCR20 0x218 /* color conversion matrix coefficient 20 */
#define VMCR21 0x21c /* color conversion matrix coefficient 21 */
#define VMCR22 0x220 /* color conversion matrix coefficient 22 */
#define VCOFFR 0x224 /* color conversion offset */
#define VCBR   0x228 /* color conversion clip */

#define VRPBR  0xc8 /* resize passband */

/* Helper functions for reading registers. */

static unsigned long read_reg(struct uio_map *ump, int reg_offs)
{
    volatile unsigned long *reg = ump->iomem;

    return reg[reg_offs / 4];
}

static void write_reg(struct uio_map *ump, unsigned long value, int reg_offs)
{
    volatile unsigned long *reg = ump->iomem;

    reg[reg_offs / 4] = value;
}

static vidix_capability_t sh_veu_cap = {
    "SuperH VEU driver",
    "Magnus Damm",
    TYPE_OUTPUT,
    { 0, 0, 0, 0 },
    2560,
    1920,
    16,
    16,
    -1,
    FLAG_UPSCALER|FLAG_DOWNSCALER,
    -1,
    -1,
    { 0, 0, 0, 0 }
};

/* global variables yuck */

static struct fb_info fbi;
static struct uio_device uio_dev;
static struct uio_map uio_mmio, uio_mem;

struct sh_veu_plane {
    unsigned long width;
    unsigned long height;
    unsigned long stride;
    unsigned long pos_x;
    unsigned long pos_y;
};

static struct sh_veu_plane _src, _dst;
static vidix_playback_t my_info;
static int fb_fd;

static int sh_veu_probe(int verbose, int force)
{
    int ret;

    ret = get_fb_info("/dev/fb0", &fbi);
    if (ret < 0)
        return ret;
    fb_fd = ret;

    if (fbi.bpp != 16) {
        printf("sh_veu: only 16bpp supported\n");
        return -1;
    }

    ret = locate_uio_device("VEU", &uio_dev);
    if (ret < 0) {
        printf("sh_veu: unable to locate matching UIO device\n");
        return ret;
    }

    ret = setup_uio_map(&uio_dev, 0, &uio_mmio);
    if (ret < 0) {
        printf("sh_veu: cannot setup MMIO\n");
        return ret;
    }

    ret = setup_uio_map(&uio_dev, 1, &uio_mem);
    if (ret < 0) {
        printf("sh_veu: cannot setup contiguous memory\n");
        return ret;
    }

    printf("sh_veu: Using %s at %s on %lux%lu %ldbpp /dev/fb0\n",
           uio_dev.name, uio_dev.path,
           fbi.width, fbi.height, fbi.bpp);

    return ret;
}

static void sh_veu_wait_irq(vidix_playback_t *info)
{
    unsigned long n_pending;

    /* Wait for an interrupt */
    read(uio_dev.fd, &n_pending, sizeof(unsigned long));

    write_reg(&uio_mmio, 0x100, VEVTR); /* ack int, write 0 to bit 0 */

    /* flush framebuffer to handle deferred io case */
    fsync(fb_fd);
}

static int sh_veu_is_veu2h(void)
{
    return uio_mmio.size == 0x27c;
}

static int sh_veu_is_veu3f(void)
{
    return uio_mmio.size == 0xcc;
}

static unsigned long sh_veu_do_scale(struct uio_map *ump,
                                     int vertical, int size_in,
                                     int size_out, int crop_out)
{
    unsigned long fixpoint, mant, frac, value, rep, vb;

    /* calculate FRAC and MANT */
    do {
        rep = mant = frac = 0;

        if (size_in == size_out) {
            if (crop_out != size_out)
                mant = 1; /* needed for cropping */
            break;
        }

        /* VEU2H special upscale */
        if (sh_veu_is_veu2h() && size_out > size_in) {
            fixpoint = (4096 * size_in) / size_out;

            mant = fixpoint / 4096;
            frac = fixpoint - (mant * 4096);
            frac &= ~0x07;

            switch (frac) {
            case 0x800:
                rep = 1;
                break;
            case 0x400:
                rep = 3;
                break;
            case 0x200:
                rep = 7;
                break;
            }

            if (rep)
                break;
        }

        fixpoint = (4096 * (size_in - 1)) / (size_out + 1);
        mant = fixpoint / 4096;
        frac = fixpoint - (mant * 4096);

        if (frac & 0x07) {
            frac &= ~0x07;
            if (size_out > size_in)
                frac -= 8; /* round down if scaling up */
            else
                frac += 8; /* round up if scaling down */
        }

    } while (0);

    /* set scale */
    value = read_reg(ump, VRFCR);
    if (vertical) {
        value &= ~0xffff0000;
        value |= ((mant << 12) | frac) << 16;
    } else {
        value &= ~0xffff;
        value |= (mant << 12) | frac;
    }
    write_reg(ump, value, VRFCR);

    /* set clip */
    value = read_reg(ump, VRFSR);
    if (vertical) {
        value &= ~0xffff0000;
        value |= ((rep << 12) | crop_out) << 16;
    } else {
        value &= ~0xffff;
        value |= (rep << 12) | crop_out;
    }
    write_reg(ump, value, VRFSR);

    /* VEU3F needs additional VRPBR register handling */
    if (sh_veu_is_veu3f()) {
        if (size_out > size_in)
            vb = 64;
        else {
            if ((mant >= 8) && (mant < 16))
                value = 4;
            else if ((mant >= 4) && (mant < 8))
                value = 2;
            else
                value = 1;

            vb = 64 * 4096 * value;
            vb /= 4096 * mant + frac;
        }

        /* set resize passband register */
        value = read_reg(ump, VRPBR);
        if (vertical) {
            value &= ~0xffff0000;
            value |= vb << 16;
        } else {
            value &= ~0xffff;
            value |= vb;
        }
        write_reg(ump, value, VRPBR);
    }

    return (((size_in * crop_out) / size_out) + 0x03) & ~0x03;
}

static void sh_veu_setup_planes(vidix_playback_t *info,
                                struct sh_veu_plane *src,
                                struct sh_veu_plane *dst)
{
    unsigned long addr, real_w, real_h;

    src->width = info->src.w;
    src->height = info->src.h;
    src->stride = (info->src.w+15) & ~15;

    dst->width = real_w = info->dest.w;
    dst->height = real_h = info->dest.h;
    dst->stride = fbi.line_length;
    dst->pos_x = info->dest.x & ~0x03;
    dst->pos_y = info->dest.y;

    if ((dst->width + dst->pos_x) > fbi.width)
        dst->width = fbi.width - dst->pos_x;

    if ((dst->height + dst->pos_y) > fbi.height)
        dst->height = fbi.height - dst->pos_y;

    addr = fbi.address;
    addr += dst->pos_x * (fbi.bpp / 8);
    addr += dst->pos_y * dst->stride;

    src->width = sh_veu_do_scale(&uio_mmio, 0, src->width,
                                 real_w, dst->width);
    src->height = sh_veu_do_scale(&uio_mmio, 1, src->height,
                                  real_h, dst->height);

    write_reg(&uio_mmio, src->stride, VESWR);
    write_reg(&uio_mmio, src->width | (src->height << 16), VESSR);
    write_reg(&uio_mmio, 0, VBSSR); /* not using bundle mode */

    write_reg(&uio_mmio, dst->stride, VEDWR);
    write_reg(&uio_mmio, addr, VDAYR);
    write_reg(&uio_mmio, 0, VDACR); /* unused for RGB */

    write_reg(&uio_mmio, 0x67, VSWPR);
    write_reg(&uio_mmio, (6 << 16) | (0 << 14) | 2 | 4, VTRCR);

    if (sh_veu_is_veu2h()) {
        write_reg(&uio_mmio, 0x0cc5, VMCR00);
        write_reg(&uio_mmio, 0x0950, VMCR01);
        write_reg(&uio_mmio, 0x0000, VMCR02);

        write_reg(&uio_mmio, 0x397f, VMCR10);
        write_reg(&uio_mmio, 0x0950, VMCR11);
        write_reg(&uio_mmio, 0x3ccd, VMCR12);

        write_reg(&uio_mmio, 0x0000, VMCR20);
        write_reg(&uio_mmio, 0x0950, VMCR21);
        write_reg(&uio_mmio, 0x1023, VMCR22);

        write_reg(&uio_mmio, 0x00800010, VCOFFR);
    }

    write_reg(&uio_mmio, 1, VEIER); /* enable interrupt in VEU */
}

static void sh_veu_blit(vidix_playback_t *info, int frame)
{
    unsigned long enable = 1;
    unsigned long addr;

    addr = uio_mem.address + info->offsets[frame];

    write_reg(&uio_mmio, addr + info->offset.y, VSAYR);
    write_reg(&uio_mmio, addr + info->offset.u, VSACR);

    /* Enable interrupt in UIO driver */
    write(uio_dev.fd, &enable, sizeof(unsigned long));

    write_reg(&uio_mmio, 1, VESTR); /* start operation */
}

static int sh_veu_init(void)
{
    write_reg(&uio_mmio, 0x100, VBSRR); /* reset VEU */
    return 0;
}

static void sh_veu_destroy(void)
{
    close(fb_fd);
}

static int sh_veu_get_caps(vidix_capability_t *to)
{
    memcpy(to, &sh_veu_cap, sizeof(vidix_capability_t));
    return 0;
}

static int sh_veu_query_fourcc(vidix_fourcc_t *to)
{
    if (to->fourcc == IMGFMT_NV12) {
        to->depth = VID_DEPTH_ALL;
        to->flags = VID_CAP_EXPAND | VID_CAP_SHRINK;
        return 0;
    }
    to->depth = to->flags = 0;
    return ENOSYS;
}


static int sh_veu_config_playback(vidix_playback_t *info)
{
    unsigned int i, y_pitch;

    switch (info->fourcc) {
    case IMGFMT_NV12:
        y_pitch = (info->src.w + 15) & ~15;

        info->offset.y = 0;
        info->offset.u = y_pitch * info->src.h;
        info->frame_size = info->offset.u + info->offset.u / 2;
        break;
    default:
        return ENOTSUP;
    }

    info->num_frames = uio_mem.size / info->frame_size;
    if (info->num_frames > VID_PLAY_MAXFRAMES)
        info->num_frames = VID_PLAY_MAXFRAMES;

    if (!info->num_frames) {
        printf("sh_veu: %d is not enough memory for %d bytes frame\n",
               (int)uio_mem.size, (int)info->frame_size);
        return ENOMEM;
    }

    info->dga_addr = uio_mem.iomem;
    info->dest.pitch.y = info->dest.pitch.u = info->dest.pitch.v = 16;

    for (i = 0; i < info->num_frames; i++)
        info->offsets[i] = info->frame_size * i;

    my_info = *info;

    printf("sh_veu: %d frames * %d bytes, total size = %d\n",
           (int)info->num_frames, (int)info->frame_size,
           (int)uio_mem.size);

    sh_veu_setup_planes(info, &_src, &_dst);

    printf("sh_veu: %dx%d->%dx%d@%dx%d -> %dx%d->%dx%d@%dx%d \n",
           (int)info->src.w, (int)info->src.h,
           (int)info->dest.w, (int)info->dest.h,
           (int)info->dest.x, (int)info->dest.y,
           (int)_src.width, (int)_src.height,
           (int)_dst.width, (int)_dst.height,
           (int)_dst.pos_x, (int)_dst.pos_y);
    return 0;
}


static int sh_veu_playback_on(void)
{
    return 0;
}

static int sh_veu_playback_off(void)
{
    return 0;
}

static int sh_veu_first_frame = 1;

static int sh_veu_frame_sel(unsigned int frame)
{
    if (!sh_veu_first_frame)
        sh_veu_wait_irq(&my_info);

    sh_veu_blit(&my_info, frame);
    sh_veu_first_frame = 0;
    return 0;
}

VDXDriver sh_veu_drv = {
    "sh_veu",
    NULL,
    .probe = sh_veu_probe,
    .get_caps = sh_veu_get_caps,
    .query_fourcc = sh_veu_query_fourcc,
    .init = sh_veu_init,
    .destroy = sh_veu_destroy,
    .config_playback = sh_veu_config_playback,
    .playback_on = sh_veu_playback_on,
    .playback_off = sh_veu_playback_off,
    .frame_sel = sh_veu_frame_sel,
};