drv_vpost.c 12.1 KB
Newer Older
W
Wayne Lin 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/**************************************************************************//**
*
* @copyright (C) 2020 Nuvoton Technology Corp. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date            Author       Notes
* 2021-4-13       Wayne        First version
*
******************************************************************************/

#include <rtconfig.h>

#if defined(BSP_USING_VPOST)

#include <rthw.h>
#include <rtdevice.h>
#include <rtdbg.h>
#include "NuMicro.h"
#include <drv_sys.h>

/* Private typedef --------------------------------------------------------------*/

W
Wayne Lin 已提交
25
#define DEF_VPOST_BUFFER_NUMBER 3
26

W
Wayne Lin 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
typedef enum
{
    eVpost_LCD,
#if defined(BSP_USING_VPOST_OSD)
    eVpost_OSD,
#endif
    eVpost_Cnt
} E_VPOST_LAYER;

struct nu_vpost
{
    struct rt_device      dev;
    char                 *name;
    E_VPOST_LAYER         layer;
    IRQn_Type             irqn;
    E_SYS_IPRST           rstidx;
    E_SYS_IPCLK           clkidx;
W
Wayne Lin 已提交
44
    uint32_t              last_commit;
W
Wayne Lin 已提交
45 46 47 48
    struct rt_device_graphic_info info;
};
typedef struct nu_vpost *nu_vpost_t;

49 50 51
static volatile uint32_t g_u32VSyncBlank = 0;
static struct rt_completion vsync_wq;

W
Wayne Lin 已提交
52 53 54 55
RT_WEAK void nu_lcd_backlight_on(void) { }

RT_WEAK void nu_lcd_backlight_off(void) { }

W
Wayne Lin 已提交
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
static struct nu_vpost nu_fbdev[eVpost_Cnt] =
{
    {
        .name = "lcd",
        .layer = eVpost_LCD,
        .irqn = IRQ_LCD,
        .rstidx = LCDRST,
        .clkidx = LCDCKEN,
    }
#if defined(BSP_USING_VPOST_OSD)
    , {
        .name = "osd",
        .layer = eVpost_OSD,
        .irqn = (IRQn_Type) - 1,
        .rstidx = SYS_IPRST_NA,
        .clkidx = SYS_IPCLK_NA,
    }
#endif
};

static rt_err_t vpost_layer_open(rt_device_t dev, rt_uint16_t oflag)
{
    nu_vpost_t psVpost = (nu_vpost_t)dev;
    RT_ASSERT(psVpost != RT_NULL);

    switch (psVpost->layer)
    {
    case eVpost_LCD:
        vpostVAStartTrigger();
        break;

#if defined(BSP_USING_VPOST_OSD)
    case eVpost_OSD:
        vpostVAStartTrigger();

        /* Set scale to 1:1 */
        vpostOSDScalingCtrl(1, 0, 0);

W
Wayne Lin 已提交
94
#if (BSP_LCD_BPP==32)
W
Wayne Lin 已提交
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
        vpostOSDSetColMask(0xff, 0xff, 0xff);
#else
        vpostOSDSetColMask(0x1f, 0x3f, 0x1f);
#endif

        /* Enable color key function */
        vpostOSDSetColKey(0, 0, 0);

        /* Configure overlay function of OSD to display OSD image */
        vpostOSDSetOverlay(DISPLAY_OSD, DISPLAY_OSD, 0);

        vpostOSDEnable();
        break;
#endif

    default:
        return -RT_ERROR;
    }

    return RT_EOK;
}

static rt_err_t vpost_layer_close(rt_device_t dev)
{
    nu_vpost_t psVpost = (nu_vpost_t)dev;
    RT_ASSERT(psVpost != RT_NULL);

    switch (psVpost->layer)
    {
    case eVpost_LCD:
#if defined(BSP_USING_VPOST_OSD)
        if (nu_fbdev[eVpost_OSD].dev.ref_count == 0)
#endif
            vpostVAStopTrigger();
        break;

#if defined(BSP_USING_VPOST_OSD)
    case eVpost_OSD:
        vpostOSDDisable();
        if (nu_fbdev[eVpost_LCD].dev.ref_count == 0)
        {
            /* Also stop displaying */
            vpostVAStopTrigger();
        }
        break;
#endif

    default:
        return -RT_ERROR;
    }

    return RT_EOK;
}

static rt_err_t vpost_layer_control(rt_device_t dev, int cmd, void *args)
{
    nu_vpost_t psVpost = (nu_vpost_t)dev;
    RT_ASSERT(psVpost != RT_NULL);

    switch (cmd)
    {
W
Wayne Lin 已提交
156 157 158 159 160 161 162 163 164 165 166 167
    case RTGRAPHIC_CTRL_POWERON:
    {
        nu_lcd_backlight_on();
    }
    break;

    case RTGRAPHIC_CTRL_POWEROFF:
    {
        nu_lcd_backlight_off();
    }
    break;

W
Wayne Lin 已提交
168 169 170 171 172 173 174 175
    case RTGRAPHIC_CTRL_GET_INFO:
    {
        struct rt_device_graphic_info *info = (struct rt_device_graphic_info *) args;
        RT_ASSERT(info != RT_NULL);
        rt_memcpy(args, (void *)&psVpost->info, sizeof(struct rt_device_graphic_info));
    }
    break;

W
Wayne Lin 已提交
176
    case RTGRAPHIC_CTRL_PAN_DISPLAY:
177 178 179 180
    {
        if (args != RT_NULL)
        {
            uint8_t *pu8BufPtr = (uint8_t *)args;
W
Wayne Lin 已提交
181

W
Wayne Lin 已提交
182
            psVpost->last_commit = g_u32VSyncBlank;
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206

            /* Pan display */
            switch (psVpost->layer)
            {
            case eVpost_LCD:
                vpostSetFrameBuffer(pu8BufPtr);
                break;

#if defined(BSP_USING_VPOST_OSD)
            case eVpost_OSD:
                vpostSetOSDBuffer(pu8BufPtr);
                break;
#endif

            default:
                return -RT_ERROR;
            }

        }
        else
            return -RT_ERROR;
    }
    break;

W
Wayne Lin 已提交
207 208
    case RTGRAPHIC_CTRL_WAIT_VSYNC:
    {
W
Wayne Lin 已提交
209
        if (args != RT_NULL)
W
Wayne Lin 已提交
210
            psVpost->last_commit = g_u32VSyncBlank + 1;
W
Wayne Lin 已提交
211

W
Wayne Lin 已提交
212
        if (psVpost->last_commit >= g_u32VSyncBlank)
W
Wayne Lin 已提交
213 214 215 216 217 218 219
        {
            rt_completion_init(&vsync_wq);
            rt_completion_wait(&vsync_wq, RT_TICK_PER_SECOND / 60);
        }
    }
    break;

W
Wayne Lin 已提交
220
    default:
W
Wayne Lin 已提交
221
        return -RT_ERROR;
W
Wayne Lin 已提交
222 223 224 225 226 227 228 229 230 231 232 233 234
    }

    return RT_EOK;
}

static rt_err_t vpost_layer_init(rt_device_t dev)
{
    nu_vpost_t psVpost = (nu_vpost_t)dev;
    RT_ASSERT(psVpost != RT_NULL);

    /* Enable VPOST engine clock. */
    nu_sys_ipclk_enable(LCDCKEN);

235 236 237 238
    rt_completion_init(&vsync_wq);
    outpw(REG_LCM_INT_CS, VPOSTB_UNDERRUN_EN | VPOSTB_DISP_F_EN);
    outpw(REG_LCM_DCCS, (inpw(REG_LCM_DCCS) | (1 << 4)));

W
Wayne Lin 已提交
239 240 241
    return RT_EOK;
}

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
static void nu_vpost_isr(int vector, void *param)
{
    /*
    #define VPOSTB_DISP_F_INT           ((UINT32)1<<31)
    #define VPOSTB_DISP_F_STATUS        (1<<30)
    #define VPOSTB_UNDERRUN_INT         (1<<29)
    #define VPOSTB_BUS_ERROR_INT        (1<<28)
    #define VPOSTB_FLY_ERR              (1<<27)
    #define VPOSTB_UNDERRUN_EN          (1<<1)
    #define VPOSTB_DISP_F_EN            (1)
    */

    uint32_t u32VpostIRQStatus = inpw(REG_LCM_INT_CS);
    if (u32VpostIRQStatus & VPOSTB_DISP_F_STATUS)
    {
        outpw(REG_LCM_INT_CS, inpw(REG_LCM_INT_CS) | VPOSTB_DISP_F_STATUS);

        g_u32VSyncBlank++;
        rt_completion_done(&vsync_wq);
    }
    else if (u32VpostIRQStatus & VPOSTB_UNDERRUN_INT)
    {
        outpw(REG_LCM_INT_CS, inpw(REG_LCM_INT_CS) | VPOSTB_UNDERRUN_INT);
    }
    else if (u32VpostIRQStatus & VPOSTB_BUS_ERROR_INT)
    {
        outpw(REG_LCM_INT_CS, inpw(REG_LCM_INT_CS) | VPOSTB_BUS_ERROR_INT);
    }
}

W
Wayne Lin 已提交
272 273 274 275 276 277 278 279
int rt_hw_vpost_init(void)
{
    int i = -1;
    rt_err_t ret;

    VPOST_T *psVpostLcmInst = vpostLCMGetInstance(VPOST_USING_LCD_IDX);
    RT_ASSERT(psVpostLcmInst != RT_NULL);

W
Wayne Lin 已提交
280
    if ((psVpostLcmInst->u32DevWidth * psVpostLcmInst->u32DevHeight) > (480 * 272))
W
Wayne Lin 已提交
281 282 283 284 285
    {
        /* LCD clock is selected from UPLL and divide to 20MHz */
        outpw(REG_CLK_DIVCTL1, (inpw(REG_CLK_DIVCTL1) & ~0xff1f) | 0xE18);

        /* LCD clock is selected from UPLL and divide to 30MHz */
W
Wayne Lin 已提交
286
        //outpw(REG_CLK_DIVCTL1, (inpw(REG_CLK_DIVCTL1) & ~0xff1f) | 0x918);
W
Wayne Lin 已提交
287 288 289 290 291 292
    }
    else
    {
        /* LCD clock is selected from UPLL and divide to 10MHz */
        outpw(REG_CLK_DIVCTL1, (inpw(REG_CLK_DIVCTL1) & ~0xff1f) | 0xE19);
    }
W
Wayne Lin 已提交
293 294 295 296 297 298 299 300 301 302 303 304 305

    /* Initial LCM */
    vpostLCMInit(VPOST_USING_LCD_IDX);

    /* Set scale to 1:1 */
    vpostVAScalingCtrl(1, 0, 1, 0, VA_SCALE_INTERPOLATION);

    for (i = eVpost_LCD; i < eVpost_Cnt; i++)
    {
        nu_vpost_t psVpost = &nu_fbdev[i];
        rt_memset((void *)&psVpost->info, 0, sizeof(struct rt_device_graphic_info));

        /* Register VPOST information */
W
Wayne Lin 已提交
306 307 308
        psVpost->info.bits_per_pixel = BSP_LCD_BPP;
        psVpost->info.pixel_format = (BSP_LCD_BPP == 32) ? RTGRAPHIC_PIXEL_FORMAT_ARGB888 : RTGRAPHIC_PIXEL_FORMAT_RGB565;
        psVpost->info.pitch = psVpostLcmInst->u32DevWidth * (BSP_LCD_BPP / 8);
W
Wayne Lin 已提交
309 310 311 312 313 314 315 316
        psVpost->info.width = psVpostLcmInst->u32DevWidth;
        psVpost->info.height = psVpostLcmInst->u32DevHeight;

        /* Get pointer of video frame buffer */
        /* Set display color depth */
        /* Note: before get pointer of frame buffer, must set display color depth first */
        if (psVpost->layer == eVpost_LCD)
        {
W
Wayne Lin 已提交
317
#if (BSP_LCD_BPP==32)
W
Wayne Lin 已提交
318 319 320 321
            vpostSetVASrc(VA_SRC_RGB888);
#else
            vpostSetVASrc(VA_SRC_RGB565);
#endif
322
            psVpost->info.framebuffer = (rt_uint8_t *)vpostGetMultiFrameBuffer(DEF_VPOST_BUFFER_NUMBER);
W
Wayne Lin 已提交
323 324 325 326 327 328
        }
#if defined(BSP_USING_VPOST_OSD)
        else if (psVpost->layer == eVpost_OSD)
        {
            vpostOSDSetWindow(0, 0, psVpost->info.width, psVpost->info.height);

W
Wayne Lin 已提交
329
#if (BSP_LCD_BPP==32)
W
Wayne Lin 已提交
330 331 332 333
            vpostSetOSDSrc(OSD_SRC_RGB888);
#else
            vpostSetOSDSrc(OSD_SRC_RGB565);
#endif
334
            psVpost->info.framebuffer = (rt_uint8_t *)vpostGetMultiOSDBuffer(DEF_VPOST_BUFFER_NUMBER);
W
Wayne Lin 已提交
335 336 337 338 339 340 341 342
        }
#endif

        if (psVpost->info.framebuffer == NULL)
        {
            rt_kprintf("Fail to get VRAM buffer.\n");
            RT_ASSERT(0);
        }
W
Wayne Lin 已提交
343 344 345 346 347
        else
        {
            uint32_t u32FBSize = psVpost->info.pitch * psVpostLcmInst->u32DevHeight;
            rt_memset(psVpost->info.framebuffer, 0, u32FBSize);
        }
W
Wayne Lin 已提交
348 349 350 351 352 353 354 355

        /* Register member functions of lcd device */
        psVpost->dev.type = RT_Device_Class_Graphic;
        psVpost->dev.init = vpost_layer_init;
        psVpost->dev.open = vpost_layer_open;
        psVpost->dev.close = vpost_layer_close;
        psVpost->dev.control = vpost_layer_control;

356
        /* Register graphic device driver */
W
Wayne Lin 已提交
357 358 359
        ret = rt_device_register(&psVpost->dev, psVpost->name, RT_DEVICE_FLAG_RDWR);
        RT_ASSERT(ret == RT_EOK);

360 361 362 363 364 365
        if (psVpost->layer == eVpost_LCD)
        {
            rt_hw_interrupt_install(psVpost->irqn, nu_vpost_isr,  psVpost, psVpost->name);
            rt_hw_interrupt_umask(psVpost->irqn);
        }

W
Wayne Lin 已提交
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
        rt_kprintf("%s's fbmem at 0x%08x.\n", psVpost->name, psVpost->info.framebuffer);
    }

    /* For saving memory bandwidth. */
    vpostLCMDeinit();

    return (int)ret;
}
INIT_DEVICE_EXPORT(rt_hw_vpost_init);


/* Support "vpost_set_osd_colkey" command line in msh mode */
static rt_err_t vpost_set_osd_colkey(int argc, char **argv)
{
    rt_uint32_t index, len, arg[4];

    rt_memset(arg, 0, sizeof(arg));
    len = (argc >= 4) ? 4 : argc;

    for (index = 0; index < (len - 1); index ++)
    {
        arg[index] = atol(argv[index + 1]);
    }

    /* Enable color key function */
    vpostOSDSetColKey(arg[0], arg[1], arg[2]);

    /* Configure overlay function of OSD to display VIDEO image */
    vpostOSDSetOverlay(DISPLAY_VIDEO, DISPLAY_OSD, 0);

    return 0;
}
MSH_CMD_EXPORT(vpost_set_osd_colkey, e.g: vpost_set_osd_colkey R G B);

/* Support "vpost_show_layer" command line in msh mode */
static rt_err_t vpost_show_layer(int argc, char **argv)
{
    rt_uint32_t index, len, arg[2];
    nu_vpost_t psVpostLayer;

    rt_memset(arg, 0, sizeof(arg));
    len = (argc >= 2) ? 2 : argc;

    for (index = 0; index < (len - 1); index ++)
    {
        arg[index] = atol(argv[index + 1]);
    }
    psVpostLayer = &nu_fbdev[arg[0]];

    return rt_device_open(&psVpostLayer->dev, RT_DEVICE_FLAG_RDWR);
}
MSH_CMD_EXPORT(vpost_show_layer, e.g: vpost_show_layer layer);

/* Support "vpost_hide_layer" command line in msh mode */
static rt_err_t vpost_hide_layer(int argc, char **argv)
{
    rt_uint32_t index, len, arg[2];
    nu_vpost_t psVpostLayer;

    rt_memset(arg, 0, sizeof(arg));
    len = (argc >= 2) ? 2 : argc;

    for (index = 0; index < (len - 1); index ++)
    {
        arg[index] = atol(argv[index + 1]);
    }
    psVpostLayer = &nu_fbdev[arg[0]];

    return rt_device_close(&psVpostLayer->dev);
}
MSH_CMD_EXPORT(vpost_hide_layer, e.g: vpost_hide_layer layer);

/* Support "vpost_fill_color" command line in msh mode */
static rt_err_t vpost_fill_color(int argc, char **argv)
{
    rt_uint32_t index, len, arg[5];
    nu_vpost_t psVpostLayer;

    rt_memset(arg, 0, sizeof(arg));
    len = (argc >= 5) ? 5 : argc;

    for (index = 0; index < (len - 1); index ++)
    {
        arg[index] = atol(argv[index + 1]);
    }

    psVpostLayer = &nu_fbdev[arg[0]];

    if (psVpostLayer->info.framebuffer != RT_NULL)
    {
        int i;
        uint32_t fill_num = psVpostLayer->info.height * psVpostLayer->info.width;
        uint32_t *fbmem_start = (uint32_t *)psVpostLayer->info.framebuffer;
        uint32_t color = (arg[1] << 16) | (arg[2] << 8) | arg[3] ;
        for (i = 0; i < fill_num; i++)
        {
            rt_memcpy((void *)&fbmem_start[i], &color, (psVpostLayer->info.bits_per_pixel / 8));
        }
    }
    return 0;
}
MSH_CMD_EXPORT(vpost_fill_color, e.g: vpost_fill_color layer R G B);

#endif /* if defined(BSP_USING_VPOST) */