123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122 |
- #include <string.h>
- #include "FreeRTOS.h"
- #include "event_groups.h"
- #include "chip.h"
- #include "board.h"
- /* LCD timing */
- #define LCD_PARAM0 0x000
- #define LCD_PARAM1 0x004
- #define LCD_PARAM2 0x008
- #define LCD_PARAM3 0x00C
- #define LCD_PARAM4 0x010
- #define LCD_PARAM5 0x014
- #define LCD_PARAM6 0x018
- #define LCD_PARAM7 0x01C
- #define LCD_PARAM8 0x020
- #define LCD_PARAM9 0x024
- #define LCD_PARAM10 0x028
- #define LCD_PARAM11 0x02C
- #define LCD_PARAM12 0x030
- #define LCD_PARAM13 0x034
- #define LCD_PARAM14 0x038
- #define LCD_PARAM15 0x03C
- #define LCD_PARAM16 0x040
- #define LCD_PARAM17 0x044
- #define LCD_PARAM18 0x048
- #define LCD_PARAM19 0x04C
- #define LCD_PARAM20 0x050
- #define LCD_PARAM21 0x054
- #define LCD_PARAM22 0x058
- /* OSD */
- #define LCD_OSD0_PARAM0 0x05C
- #define LCD_OSD0_PARAM1 0x060
- #define LCD_OSD0_PARAM2 0x064
- #define LCD_OSD0_PARAM3 0x068
- #define LCD_OSD0_PARAM4 0x06C
- #define LCD_OSD0_PARAM5 0x070
- #define LCD_OSD1_PARAM0 0x074
- #define LCD_OSD1_PARAM1 0x078
- #define LCD_OSD1_PARAM2 0x07C
- #define LCD_OSD1_PARAM3 0x080
- #define LCD_OSD1_PARAM4 0x084
- #define LCD_OSD1_PARAM5 0x088
- #define LCD_OSD01_PARAM 0x08C
- #define LCD_OSD_COEF_SYNC 0x090
- #define LCD_OSD0_PARAM2_RD 0x280
- #define LCD_OSD0_PARAM3_RD 0x284
- #define LCD_OSD0_PARAM4_RD 0x288
- #define LCD_OSD1_PARAM2_RD 0x28C
- #define LCD_OSD1_PARAM3_RD 0x290
- #define LCD_OSD1_PARAM4_RD 0x294
- /* Dithering */
- #define LCD_DITHERING_CFG0 0x0BC
- #define LCD_DITHERING_CFG1 0x0C0
- #define LCD_DITHERING_CFG2 0x0C4
- #define LCD_INTR_CLR 0x0C8
- #define LCD_STATUS_REG 0x0CC
- #define LCD_R_BIT_ORDER 0x130
- #define LCD_G_BIT_ORDER 0x134
- #define LCD_B_BIT_ORDER 0x138
- #define LCD_GAMMA_REG 0x13c
- /* CPU screen & SRGB screen */
- #define LCD_SRGB_CFG 0x200
- #define LCD_CPU_SCR_SOFT_REG 0x0E0
- #define LCD_CPU_SCR_CTRL_REG 0x0E4
- #define LCD_ADR_CLR_DATA_REG0 0x0E8
- #define LCD_ADR_CLR_DATA_REG1 0x0EC
- #define LCD_ADR_CLR_DATA_REG2 0x0F0
- #define LCD_ADR_CLR_DATA_REG3 0x0F4
- #define LCD_ADR_CLR_DATA_REG4 0x0F8
- #define LCD_ADR_CLR_DATA_REG5 0x0FC
- #define LCD_ADR_CLR_DATA_REG6 0x100
- #define LCD_ADR_CLR_DATA_REG7 0x104
- #define LCD_ADR_CLR_DATA_REG8 0x108
- #define LCD_ADR_CLR_DATA_REG9 0x10C
- #define LCD_ADR_CLR_DATA_REGA 0x110
- #define LCD_ADR_CLR_DATA_REGB 0x114
- #define LCD_ADR_CLR_DATA_REGC 0x118
- #define LCD_ADR_CLR_DATA_REGD 0x11C
- #define LCD_ADR_CLR_DATA_REGE 0x120
- #define LCD_ADR_CLR_DATA_REGF 0x124
- #define LCD_CPU_SCREEN_STATUS 0x128
- #define LCD_OSD_CCM_REG0 0x208
- #define LCD_OSD_CCM_REG1 0x20C
- #define LCD_OSD_CCM_REG2 0x210
- #define LCD_OSD_CCM_REG3 0x214
- #define LCD_OSD_CCM_REG4 0x218
- #define LCD_OSD_CCM_REG5 0x21C
- #define LCD_CCM_EN_REG0 0x238
- #define LCD_CCM_EN_REG1 0x23C
- #define LCD_CCM_EN_REG2 0x240
- #define LCD_CCM_EN_REG3 0x244
- #define LCD_CCM_EN_REG4 0x248
- #define LCD_CCM_EN_REG5 0x24C
- #define LCD_CCM_EN_REG 0x250
- #define LCD_ITU_EAV_CODE_CFG 0x254
- #define LCD_ITU_SAV_CODE_CFG 0x258
- #define LCD_HLOCK_CFG_RG 0x25C
- #if defined(AWTK) || LCD_ROTATE_ANGLE != LCD_ROTATE_ANGLE_0
- #define FB_COUNT 3
- #else
- #define FB_COUNT 2
- #endif
- static void *fb_buf = NULL;
- static uint32_t fb_addr = 0;
- struct ark_lcd_timing
- {
- uint32_t vs_start;
- uint32_t hs_start;
- uint32_t hd; //width
- uint32_t vd; //height
- uint32_t vbp;
- uint32_t vfp;
- uint32_t vsw;
- uint32_t hbp;
- uint32_t hfp;
- uint32_t hsw;
- };
- #if configUSE_16_BIT_TICKS == 1
- #define MAX_VSYNC_WAIT_TASKS 8
- #else
- #define MAX_VSYNC_WAIT_TASKS 24
- #endif
- struct ark_lcd_data
- {
- struct ark_lcd_timing timing;
- EventGroupHandle_t vsync_waitq;
- void *waitq_tasks[MAX_VSYNC_WAIT_TASKS];
- uint32_t waitbits;
- };
- struct ark_lcd_data *g_lcd = NULL;
- static int g_osdConfig[LCD_OSD_NUMS] = {0};
- #ifdef AWTK
- #include "rtos.h"
- #include "fb_queue.h"
- #include "xm_base.h"
- static queue_s lcd_fb_ready; // 已准备好的LCD帧
- static queue_s lcd_fb_free; // 空闲
- static fb_queue_s fb_queue_unit[FB_COUNT];
- #define ENABLE_FB_DIRTY_RECTS_COPY
- #ifdef ENABLE_FB_DIRTY_RECTS_COPY
- int vg_set_surface(unsigned int *surface_addr, int surface_size,
- unsigned int surface_width,
- unsigned int surface_height,
- unsigned int surface_bpp, unsigned int surface_stride);
- #endif
- void fb_queue_init (void)
- {
- queue_initialize (&lcd_fb_ready);
- queue_initialize (&lcd_fb_free);
- memset (fb_queue_unit, 0, sizeof(fb_queue_unit));
- for (int i = 0; i < FB_COUNT; i ++)
- {
- fb_queue_unit[i].fb_base = i * FB_SIZE + (unsigned int)fb_buf;
- queue_insert ((queue_s *)(fb_queue_unit + i), &lcd_fb_free);
- }
-
- #ifdef ENABLE_FB_DIRTY_RECTS_COPY
- {
- unsigned int fb_addr[FB_COUNT];
- for (int i = 0; i < FB_COUNT; i ++)
- {
- fb_addr[i] = i * FB_SIZE + (unsigned int)fb_buf;
- }
- vg_set_surface (fb_addr, FB_COUNT, OSD_WIDTH, OSD_HEIGHT, LCD_BPP, OSD_WIDTH * LCD_BPP/8);
- }
- #endif
- }
- void fb_queue_exit (void)
- {
- }
- fb_queue_s *fb_queue_get_free_unit(void)
- {
- fb_queue_s *unit = NULL;
- XM_lock();
- if(!queue_empty (&lcd_fb_free))
- {
- unit = (fb_queue_s *)queue_delete_next (&lcd_fb_free);
- }
- XM_unlock();
- return unit;
- }
- void fb_queue_set_free (fb_queue_s *unit)
- {
- XM_lock();
- queue_insert ((queue_s *)unit, &lcd_fb_free);
- XM_unlock();
- }
- void fb_queue_set_free_isr (fb_queue_s *unit)
- {
- queue_insert ((queue_s *)unit, &lcd_fb_free);
- }
- void fb_queue_set_ready (fb_queue_s *unit)
- {
- XM_lock();
- queue_insert ((queue_s *)unit, &lcd_fb_ready);
- XM_unlock();
- }
- fb_queue_s *fb_queue_get_ready_unit_isr(void)
- {
- fb_queue_s *unit = NULL;
- if(!queue_empty (&lcd_fb_ready))
- {
- unit = (fb_queue_s *)queue_delete_next (&lcd_fb_ready);
- }
- return unit;
- }
- fb_queue_s *fb_queue_get_ready_unit(void)
- {
- fb_queue_s *unit = NULL;
- XM_lock();
- if(!queue_empty (&lcd_fb_ready))
- {
- unit = (fb_queue_s *)queue_delete_next (&lcd_fb_ready);
- }
- XM_unlock();
- return unit;
- }
- fb_queue_s *fb_queue_get_unit_from_base(unsigned int base)
- {
- for (int i = 0; i < FB_COUNT; i ++)
- {
- if(fb_queue_unit[i].fb_base == base)
- return fb_queue_unit + i;
- }
- return NULL;
- }
- #endif
- static void ark_lcd_writel(uint32_t reg, uint32_t mask, uint8_t offset, uint32_t val)
- {
- uint32_t tmp = readl(REGS_LCD_BASE+reg);
- tmp &= (~(mask << offset));
- tmp |= (val << offset);
- writel(tmp, REGS_LCD_BASE+reg);
- }
- int ark_lcd_set_osd_sync(LCD_OSD_LAYER osd)
- {
- if(osd >= LCD_OSD_NUMS) {
- printf("ERR: %s, Invalid osd%d\n", __func__, osd);
- return -1;
- }
- if(osd == LCD_OSD0)
- writel(readl(REGS_LCD_BASE+LCD_OSD_COEF_SYNC)|(1<<0), REGS_LCD_BASE+LCD_OSD_COEF_SYNC);
- else if(osd == LCD_OSD1)
- writel(readl(REGS_LCD_BASE+LCD_OSD_COEF_SYNC)|(1<<1), REGS_LCD_BASE+LCD_OSD_COEF_SYNC);
- else if(osd == LCD_OSD_NUMS)
- writel(readl(REGS_LCD_BASE+LCD_OSD_COEF_SYNC)|(3<<0), REGS_LCD_BASE+LCD_OSD_COEF_SYNC);
- return 0;
- }
- static int ark_lcd_set_osd_stride(LCD_OSD_LAYER osd, uint32_t width)
- {
- uint32_t reg = LCD_OSD0_PARAM5 + osd*0x18;
- if(osd >= LCD_OSD_NUMS) {
- printf("ERR: %s, Invalid osd%d\n", __func__, osd);
- return -1;
- }
- if(width > 0xFFF) {
- printf("ERR: %s, Invalid width:0x%x\n", __func__, width);
- return -1;
- }
- ark_lcd_writel(reg, 0xFFF, 0, width);
- return 0;
- }
- int ark_lcd_get_osd_size(LCD_OSD_LAYER osd, uint32_t *width, uint32_t *height)
- {
- uint32_t reg = LCD_OSD0_PARAM0 + osd*0x18;
- uint32_t val;
- if(osd>= LCD_OSD_NUMS) {
- printf("ERR: %s, Invalid osd%d\n", __func__, osd);
- return -1;
- }
- val = readl(REGS_LCD_BASE+reg);
- *width = (val >> 6) & 0xFFF;
- *height = (val >> 18) & 0xFFF;
- return 0;
- }
- int ark_lcd_set_osd_size(LCD_OSD_LAYER osd, uint32_t width, uint32_t height)
- {
- uint32_t reg = LCD_OSD0_PARAM0 + osd*0x18;
- if(osd>= LCD_OSD_NUMS) {
- printf("ERR: %s, Invalid osd%d\n", __func__, osd);
- return -1;
- }
- width &= 0xFFF;
- height &= 0xFFF;
- ark_lcd_writel(reg, 0xFFFFFF, 6, (height<<12)|width);
- ark_lcd_set_osd_stride(osd, width);
- return 0;
- }
- int ark_lcd_get_osd_format(LCD_OSD_LAYER osd, LCD_OSD_FORMAT *format)
- {
- uint32_t reg = LCD_OSD0_PARAM0 + osd*0x18;
- uint32_t val;
- if(osd >= LCD_OSD_NUMS) {
- printf("ERR: %s, Invalid osd%d\n", __func__, osd);
- return -1;
- }
- val = readl(REGS_LCD_BASE+reg);
- *format = (val>>3)&0x07;
- if(*format >= LCD_OSD_FORAMT_NUMS) {
- printf("ERR: %s, Invalid osd%d format:%d\n", __func__, osd, *format);
- return -1;
- }
- return 0;
- }
- int ark_lcd_set_osd_format(LCD_OSD_LAYER osd, LCD_OSD_FORMAT format)
- {
- uint32_t reg = LCD_OSD0_PARAM0 + osd*0x18;
- if(osd >= LCD_OSD_NUMS) {
- printf("ERR: %s, Invalid osd%d\n", __func__, osd);
- return -1;
- }
- if(format >= LCD_OSD_FORAMT_NUMS) {
- printf("ERR: %s, Invalid osd%d format:%d\n", __func__, osd, format);
- return -1;
- }
- ark_lcd_writel(reg, 0x7, 3, format);
- return 0;
- }
- int ark_lcd_osd_enable(LCD_OSD_LAYER osd, uint8_t enable)
- {
- uint32_t reg = LCD_OSD0_PARAM0 + osd*0x18;
- if(osd >= LCD_OSD_NUMS) {
- printf("ERR: %s, Invalid osd%d\n", __func__, osd);
- return -1;
- }
- ark_lcd_writel(reg, 0x1, 1, (enable?1:0));
- return 0;
- }
- int ark_lcd_get_osd_enable(LCD_OSD_LAYER osd)
- {
- uint32_t reg = LCD_OSD0_PARAM0 + osd*0x18;
- uint32_t val;
- if(osd >= LCD_OSD_NUMS) {
- printf("ERR: %s, Invalid osd%d\n", __func__, osd);
- return -1;
- }
- val = readl(REGS_LCD_BASE + reg);
- return (val >> 1) & 1;
- }
- int ark_lcd_osd_coeff_enable(LCD_OSD_LAYER osd, uint8_t enable)
- {
- uint32_t reg = LCD_OSD0_PARAM0 + osd*0x18;
- if(osd >= LCD_OSD_NUMS) {
- printf("ERR: %s, Invalid osd%d\n", __func__, osd);
- return -1;
- }
- ark_lcd_writel(reg, 0x0, 1, (enable?1:0));
- return 0;
- }
- int ark_lcd_osd_set_coeff(LCD_OSD_LAYER osd, uint32_t value)
- {
- uint32_t reg = LCD_OSD0_PARAM1 + osd*0x18;
- if(osd >= LCD_OSD_NUMS) {
- printf("ERR: %s, Invalid osd%d\n", __func__, osd);
- return -1;
- }
- ark_lcd_writel(reg, 0xFF, 24, value);
- return 0;
- }
- int ark_lcd_set_osd_possition(LCD_OSD_LAYER osd, uint32_t h, uint32_t v)
- {
- uint32_t reg = LCD_OSD0_PARAM1 + osd*0x18;
- if(osd >= LCD_OSD_NUMS) {
- printf("ERR: %s, Invalid osd%d\n", __func__, osd);
- return -1;
- }
- if((v > 0xFFF) || (h > 0xFFF)) {
- printf("ERR: %s, Invalid v:0x%x or h:0x%x\n", __func__, v, h);
- return -1;
- }
- ark_lcd_writel(reg, 0xFFFFFF, 0, ((v<<12)|h));
- return 0;
- }
- int ark_lcd_get_osd_yaddr(LCD_OSD_LAYER osd, uint32_t *yaddr)
- {
- uint32_t reg = LCD_OSD0_PARAM2 + osd*0x18;
- if(osd >= LCD_OSD_NUMS) {
- printf("ERR: %s, Invalid osd%d\n", __func__, osd);
- return -1;
- }
- *yaddr = readl(REGS_LCD_BASE+reg);
- return 0;
- }
- int ark_lcd_set_osd_yaddr(LCD_OSD_LAYER osd, uint32_t yaddr)
- {
- uint32_t reg = LCD_OSD0_PARAM2 + osd*0x18;
- if(osd >= LCD_OSD_NUMS) {
- printf("ERR: %s, Invalid osd%d\n", __func__, osd);
- return -1;
- }
- #if LCD_V_FLIP
- LCD_OSD_FORMAT format;
- uint32_t width, height;
- ark_lcd_get_osd_format(osd, &format);
- ark_lcd_get_osd_size(osd, &width, &height);
- if (format == LCD_OSD_FORAMT_YUV420)
- yaddr += width * (height - 1);
- else if (format == LCD_OSD_FORAMT_ARGB888)
- yaddr += width * (height - 1) * 4;
- else if (format == LCD_OSD_FORAMT_RGB565)
- yaddr += width * (height - 1) * 2;
- #endif
- writel(yaddr, REGS_LCD_BASE+reg);
-
- return 0;
- }
- int ark_lcd_set_osd_uaddr(LCD_OSD_LAYER osd, uint32_t uaddr)
- {
- uint32_t reg = LCD_OSD0_PARAM3 + osd*0x18;
- if(osd >= LCD_OSD_NUMS) {
- printf("ERR: %s, Invalid osd%d\n", __func__, osd);
- return -1;
- }
- #if LCD_V_FLIP
- LCD_OSD_FORMAT format;
- uint32_t width, height;
- LCD_OSD_YUV420_MODE yuv420_mode;
-
- ark_lcd_get_osd_format(osd, &format);
- ark_lcd_get_osd_size(osd, &width, &height);
- ark_lcd_get_osd_yuv420_mode(osd, &yuv420_mode);
- if (format == LCD_OSD_FORAMT_YUV420) {
- if (yuv420_mode == LCD_OSD_Y_U_V420)
- uaddr += width * (height - 2) / 4;
- else
- uaddr += width * (height - 2) / 2;
- }
- #endif
- writel(uaddr, REGS_LCD_BASE+reg);
- return 0;
- }
- int ark_lcd_set_osd_vaddr(LCD_OSD_LAYER osd, uint32_t vaddr)
- {
- uint32_t reg = LCD_OSD0_PARAM4 + osd*0x18;
- if(osd >= LCD_OSD_NUMS) {
- printf("ERR: %s, Invalid osd%d\n", __func__, osd);
- return -1;
- }
- #if LCD_V_FLIP
- LCD_OSD_FORMAT format;
- uint32_t width, height;
- LCD_OSD_YUV420_MODE yuv420_mode;
-
- ark_lcd_get_osd_format(osd, &format);
- ark_lcd_get_osd_size(osd, &width, &height);
- ark_lcd_get_osd_yuv420_mode(osd, &yuv420_mode);
- if (format == LCD_OSD_FORAMT_YUV420 && yuv420_mode == LCD_OSD_Y_U_V420)
- vaddr += width * (height - 2) / 4;
- #endif
- writel(vaddr, REGS_LCD_BASE+reg);
- return 0;
- }
- int ark_lcd_set_osd_mult_coef(LCD_OSD_LAYER osd, uint32_t value)
- {
- uint32_t reg = LCD_OSD0_PARAM5 + osd*0x18;
- if(osd >= LCD_OSD_NUMS) {
- printf("ERR: %s, Invalid osd%d\n", __func__, osd);
- return -1;
- }
- if(value > 0x7F) {
- printf("ERR: %s, Invalid value:0x%x\n", __func__, value);
- return -1;
- }
- ark_lcd_writel(reg, 0x7F, 24, value);
- return 0;
- }
- int ark_lcd_set_osd_h_offset(LCD_OSD_LAYER osd, uint32_t offset)
- {
- uint32_t reg = LCD_OSD0_PARAM5 + osd*0x18;
- if(osd >= LCD_OSD_NUMS) {
- printf("ERR: %s, Invalid osd%d\n", __func__, osd);
- return -1;
- }
- if(offset > 0xFFF) {
- printf("ERR: %s, Invalid offset:0x%x\n", __func__, offset);
- return -1;
- }
- ark_lcd_writel(reg, 0xFFF, 12, offset);
- return 0;
- }
- int ark_lcd_set_osd_yuv420_mode(LCD_OSD_LAYER osd, LCD_OSD_YUV420_MODE mode)
- {
- uint32_t reg = LCD_OSD01_PARAM;
- if(osd >= LCD_OSD_NUMS) {
- printf("ERR: %s, Invalid osd%d\n", __func__, osd);
- return -1;
- }
- if(mode > LCD_OSD_Y_UV420) {
- printf("ERR: %s, Invalid osd%d mode:%d\n", __func__, osd, mode);
- return -1;
- }
- if (osd == LCD_OSD0)
- ark_lcd_writel(reg, 0x1, 12, mode);
- else
- ark_lcd_writel(reg, 0x1, 14, mode);
- return 0;
- }
- int ark_lcd_get_osd_yuv420_mode(LCD_OSD_LAYER osd, LCD_OSD_YUV420_MODE *mode)
- {
- uint32_t reg = LCD_OSD01_PARAM;
- if(osd >= LCD_OSD_NUMS) {
- printf("ERR: %s, Invalid osd%d\n", __func__, osd);
- return -1;
- }
- if (osd == LCD_OSD0)
- *mode = (readl(REGS_LCD_BASE + reg) >> 12) & 1;
- else
- *mode = (readl(REGS_LCD_BASE + reg) >> 14) & 1;
- return 0;
- }
- int ark_lcd_enable(uint8_t enable)
- {
- ark_lcd_writel(LCD_PARAM0, 0x01, 0, (enable?1:0));
- return 0;
- }
- int ark_lcd_wait_for_vsync(void)
- {
- struct ark_lcd_data *lcd = g_lcd;
- int i;
- if(!lcd) {
- printf("ERR: %s, Invalid lcd(NULL)\n", __func__);
- return -EINVAL;
- }
- #if LCD_INTERFACE_TYPE != LCD_INTERFACE_CPU
- portENTER_CRITICAL();
- for (i = 0; i < MAX_VSYNC_WAIT_TASKS; i++) {
- if (lcd->waitq_tasks[i] == xTaskGetCurrentTaskHandle()) {
- lcd->waitbits |= 1 << i;
- break;
- } else if (lcd->waitq_tasks[i] == NULL) {
- lcd->waitq_tasks[i] = xTaskGetCurrentTaskHandle();
- lcd->waitbits |= 1 << i;
- break;
- }
- }
- portEXIT_CRITICAL();
- if (i == MAX_VSYNC_WAIT_TASKS) {
- printf("ERR: %s, waitq_tasks full\n", __func__);
- return -1;
- }
- xEventGroupClearBits(lcd->vsync_waitq, 1 << i);
- xEventGroupWaitBits(lcd->vsync_waitq, 1 << i, pdFALSE, pdFALSE, pdMS_TO_TICKS(50));
- portENTER_CRITICAL();
- lcd->waitbits &= ~(1 << i);
- portEXIT_CRITICAL();
- #endif
- return 0;
- }
- int ark_lcd_set_osd_info_atomic(LCD_OSD_LAYER osd, LcdOsdInfo *info)
- {
- portENTER_CRITICAL();
- ark_lcd_set_osd_possition(osd, info->x, info->y);
- ark_lcd_set_osd_size(osd, info->width, info->height);
- ark_lcd_set_osd_format(osd, info->format);
- ark_lcd_set_osd_yaddr(osd, info->yaddr);
- ark_lcd_set_osd_uaddr(osd, info->uaddr);
- ark_lcd_set_osd_vaddr(osd, info->vaddr);
- ark_lcd_set_osd_sync(osd);
- g_osdConfig[osd - LCD_OSD0] = 1;
- portEXIT_CRITICAL();
- return 0;
- }
- int ark_lcd_get_osd_info_atomic_isactive(LCD_OSD_LAYER osd)
- {
- int active;
- portENTER_CRITICAL();
- active = !g_osdConfig[osd - LCD_OSD0];
- portEXIT_CRITICAL();
- return active;
- }
- uint32_t ark_lcd_get_virt_addr(void)
- {
- return (uint32_t)fb_buf;
- }
- static int ark_lcd_timing_init(struct ark_lcd_data *lcd)
- {
- struct ark_lcd_timing timing;
- uint32_t VS_START, HS_START, HD, VD;
- uint32_t VBP, VFP, VSW, TV;
- uint32_t HBP, HFP, HSW, TH;
- uint32_t lcd_param0, lcd_param1, lcd_param2, lcd_param3 ;
- uint32_t lcd_param4, lcd_param5, lcd_param6, lcd_param7 ;
- uint32_t lcd_param8, lcd_param9, lcd_param10, lcd_param11 ;
- uint32_t lcd_param12, lcd_param13, lcd_param14, lcd_param15 ;
- uint32_t lcd_param16, lcd_param17;
- uint8_t stop_lcd = 0 ;
- if(!lcd) {
- printf("ERR: %s, lcd == NULL\n", __func__);
- return -1;
- }
- timing = lcd->timing;
- VS_START = timing.vs_start;
- HS_START = timing.hs_start;
- HD = timing.hd;
- VD = timing.vd;
- VBP = timing.vbp;
- VFP = timing.vfp;
- VSW = timing.vsw;
- TV =(VS_START+VSW+VBP+VD+VFP);
- HBP = timing.hbp;
- HFP = timing.hfp;
- HSW = timing.hsw;
- TH = (HS_START+HSW+HBP+HD+HFP);
- #define lcd_enable 0 // 1
- #define screen_width HD
- #define rgb_pad_mode LCD_WIRING_MODE
- #define direct_enable 0
- #define mem_lcd_enable 0
- #define range_coeff_y 0
- #define range_coeff_uv 0
- #define lcd_done_intr_enable 1
- #define dac_for_video 0
- #define dac_for_cvbs 0
- #define lcd_interlace_flag 0
- #define screen_type 1 //pRGB_i 0 pRGB_p 1 sRGB_p 2 ITU656 3
- #define VSW1_enable 0
- #define LcdVComp 0
- #define itu_pal_ntsc 0
- #define hsync_ivs 1
- #define vsync_ivs 1
- #define lcd_ac_ivs 0
- #define test_on_flag 0
- #define back_color (0x00<<16)|(0x00<<8)|(0x00)
- #define DEN_h_rise (HFP+HSW+HBP)
- #define DEN_h_fall (HFP+HSW+HBP+HD)//(20+800*1)
- #define DEN0_v_rise (VFP+VSW+VBP)
- #define DEN0_v_fall (VFP+VSW+VBP+VD)
- #define DEN1_v_rise (VFP+VSW+VBP)
- #define DEN1_v_fall (VFP+VSW+VBP+VD)
- // ֡Ƶ = LCDʱÖÓ/(CPL * LPS)
- // ÿÐÐʱÖÓÖÜÆÚ¸öÊý
- #define CPL (TH) // clock cycles per line max 4096 ; from register timing2 16
- #define LPS (TV) // Lines per screen value ; from register timing1 0
- #define HSW_rise (HFP)
- #define HSW_fall (HFP+HSW)
- #define VSW0_v_rise (VFP) // Vertical sync width value ; from register timing1 10
- #define VSW0_v_fall (VFP+VFP) // Vertical sync width value ; from register timing1 10
- #define VSW0_h_rise (HFP)
- #define VSW0_h_fall (HFP+HSW)
- #define VSW1_v_rise (VFP) // Vertical sync width value ; from register timing1 10
- #define VSW1_v_fall (VFP+VSW) // Vertical sync width value ; from register timing1 10
- #define VSW1_h_rise (HFP)
- #define VSW1_h_fall (HFP+HSW)
- writel(0, REGS_LCD_BASE+LCD_PARAM0);
- lcd_param0 =
- lcd_enable |
- (screen_width<<1) |
- (rgb_pad_mode<<13) |
- (direct_enable<<16) |
- (mem_lcd_enable<<17)|
- (range_coeff_y<<18) |
- (range_coeff_uv<<22)|
- (lcd_done_intr_enable<<26)|
- (dac_for_video<<27) |
- (dac_for_cvbs<<28) |
- (1<<30) |
- (stop_lcd<<31);
- lcd_param1 =
- (lcd_interlace_flag<<0) |
- (screen_type<<1) |
- (VSW1_enable<<13) |
- (LcdVComp<<14) |
- (itu_pal_ntsc<<15) |
- (hsync_ivs<<17) |
- (vsync_ivs<<18) |
- (lcd_ac_ivs<<19) |
- (1<<21) |
- (1<<23) |
- (test_on_flag<<31);
- lcd_param2 =
- (back_color<<0) |
- (1<<24) |
- (1<<25);
- lcd_param3 =
- (DEN_h_rise<<0) |
- (DEN_h_fall<<12) ;
- lcd_param4 =
- (DEN0_v_rise<<0) |
- (DEN0_v_fall<<12) ;
- lcd_param5 =
- (DEN1_v_rise<<0) |
- (DEN1_v_fall<<12 ) ;
- lcd_param6 =
- (CPL<<0) | // clock cycles per line max 4096 ; from register timing2 16
- (LPS<<12) ; // Lines per screen value ; from register timing1 0
- lcd_param7 =
- (HSW_rise<<0)|
- (HSW_fall<<12) ;
- lcd_param8 =
- (VSW0_v_rise<<0)| // Vertical sync width value ; from register timing1 10
- (VSW0_v_fall<<12) ; // Vertical sync width value ; from register timing1 10
- lcd_param9 =
- (VSW0_h_rise <<0 )|
- (VSW0_h_fall <<12 ) ;
- lcd_param10 =
- (VSW1_v_rise<<0)|
- (VSW1_v_fall<<12) ;
- lcd_param11 =
- (VSW1_h_rise<<0)|
- (VSW1_h_fall<<12) ;
- lcd_param12 = 66 | (129<<9) | (25<<18);
- lcd_param13 = 38 | (74<<9) | (112<<18);
- lcd_param14 = 112 | (94<<9) | (18<<18);
-
- lcd_param15 = 256 | (0<<9) | (351<<18);
- lcd_param16 = 256 | (86<<9) | (179<<18);
- lcd_param17 = 256 | (443<<9) | (0<<19);
- writel(lcd_param1, REGS_LCD_BASE+LCD_PARAM1);
- writel(lcd_param2, REGS_LCD_BASE+LCD_PARAM2);
- writel(lcd_param3, REGS_LCD_BASE+LCD_PARAM3);
- writel(lcd_param4, REGS_LCD_BASE+LCD_PARAM4);
- writel(lcd_param5, REGS_LCD_BASE+LCD_PARAM5);
- writel(lcd_param6, REGS_LCD_BASE+LCD_PARAM6);
- writel(lcd_param7, REGS_LCD_BASE+LCD_PARAM7);
- writel(lcd_param8, REGS_LCD_BASE+LCD_PARAM8);
- writel(lcd_param9, REGS_LCD_BASE+LCD_PARAM9);
- writel(lcd_param10, REGS_LCD_BASE+LCD_PARAM10);
- writel(lcd_param11, REGS_LCD_BASE+LCD_PARAM11);
- writel(lcd_param12, REGS_LCD_BASE+LCD_PARAM12);
- writel(lcd_param13, REGS_LCD_BASE+LCD_PARAM13);
- writel(lcd_param14, REGS_LCD_BASE+LCD_PARAM14);
- writel(lcd_param15, REGS_LCD_BASE+LCD_PARAM15);
- writel(lcd_param16, REGS_LCD_BASE+LCD_PARAM16);
- writel(lcd_param17, REGS_LCD_BASE+LCD_PARAM17);
- writel(lcd_param0, REGS_LCD_BASE+LCD_PARAM0);
- #if LCD_WIRING_BIT_ORDER == LCD_WIRING_BIT_ORDER_LSB
- writel((0<<21|1<<18|2<<15|3<<12|4<<9|5<<6|6<<3|7<<0), REGS_LCD_BASE+LCD_R_BIT_ORDER);
- writel((0<<21|1<<18|2<<15|3<<12|4<<9|5<<6|6<<3|7<<0), REGS_LCD_BASE+LCD_G_BIT_ORDER);
- writel((0<<21|1<<18|2<<15|3<<12|4<<9|5<<6|6<<3|7<<0), REGS_LCD_BASE+LCD_B_BIT_ORDER);
- #elif LCD_WIRING_BIT_ORDER == LCD_WIRING_BIT_ORDER_MSB
- writel((7<<21|6<<18|5<<15|4<<12|3<<9|2<<6|1<<3|0<<0), REGS_LCD_BASE+LCD_R_BIT_ORDER);
- writel((7<<21|6<<18|5<<15|4<<12|3<<9|2<<6|1<<3|0<<0), REGS_LCD_BASE+LCD_G_BIT_ORDER);
- writel((7<<21|6<<18|5<<15|4<<12|3<<9|2<<6|1<<3|0<<0), REGS_LCD_BASE+LCD_B_BIT_ORDER);
- #endif
-
- return 0;
- }
- static int ark_lcd_hw_init()
- {
- uint32_t HD, VD;
- #ifdef AWTK
- HD = OSD_WIDTH;
- VD = OSD_HEIGHT;
- #else
- HD = LCD_WIDTH;
- VD = LCD_HEIGHT;
- #endif
- #if LCD_H_FLIP
- uint8_t osd_0_hflip = 1;
- uint8_t osd_1_hflip = 1;
- #else
- uint8_t osd_0_hflip = 0;
- uint8_t osd_1_hflip = 0;
- #endif
- #if LCD_V_FLIP
- uint8_t osd_0_vflip = 1;
- uint8_t osd_1_vflip = 1;
- #else
- uint8_t osd_0_vflip = 0;
- uint8_t osd_1_vflip = 0;
- #endif
- uint8_t osd_0_uvfirst = 0;
- uint8_t osd_0_yuv420mode = 1;
- //uint8_t osd_0_yuv422mode = 0;
- uint32_t osd_01_param =
- //osd_0_yuv422mode<<20|
- osd_0_hflip<<16|
- osd_0_vflip<<17|
- osd_1_hflip<<18|
- osd_1_vflip<<19|
- osd_0_yuv420mode<<12|
- osd_0_uvfirst<<13;
- writel(osd_01_param, REGS_LCD_BASE+LCD_OSD01_PARAM);
- //osd0 init
- ark_lcd_set_osd_size(LCD_OSD0, HD, VD);
- #if LCD_BPP == 32
- ark_lcd_set_osd_format(LCD_OSD0, LCD_OSD_FORAMT_ARGB888);
- #elif LCD_BPP == 16
- ark_lcd_set_osd_format(LCD_OSD0, LCD_OSD_FORAMT_RGB565);
- #endif
- ark_lcd_set_osd_possition(LCD_OSD0, 0, 0);
- ark_lcd_set_osd_yaddr(LCD_OSD0, fb_addr);
- ark_lcd_set_osd_h_offset(LCD_OSD0, 0);
- ark_lcd_set_osd_mult_coef(LCD_OSD0, 64);
- //ark_lcd_osd_enable(LCD_OSD0, 1);
- ark_lcd_set_osd_sync(LCD_OSD0);
- //osd1 init
- ark_lcd_set_osd_size(LCD_OSD1, HD, VD);
- #if LCD_BPP == 32
- ark_lcd_set_osd_format(LCD_OSD1, LCD_OSD_FORAMT_ARGB888);
- ark_lcd_osd_coeff_enable(LCD_OSD1, 0);
- #elif LCD_BPP == 16
- ark_lcd_set_osd_format(LCD_OSD1, LCD_OSD_FORAMT_RGB565);
- ark_lcd_osd_coeff_enable(LCD_OSD1, 1);
- ark_lcd_osd_set_coeff(LCD_OSD1, 255);
- #endif
- ark_lcd_set_osd_possition(LCD_OSD1, 0, 0);
- ark_lcd_set_osd_h_offset(LCD_OSD1, 0);
- ark_lcd_set_osd_mult_coef(LCD_OSD1, 64);
- ark_lcd_set_osd_yaddr(LCD_OSD1, fb_addr);
- ark_lcd_osd_enable(LCD_OSD1, 1);
- ark_lcd_set_osd_sync(LCD_OSD1);
- memset(fb_buf, 0, FB_SIZE);
- CP15_clean_dcache_for_dma(fb_addr, fb_addr + FB_SIZE);
- return 0;
- }
- static unsigned int ark_lcd_set_clk_below16(uint32_t freq)
- {
- int div;
- uint32_t parent_rate;
- if (freq < 14000000) {
- vSysctlConfigure(SYS_VOU_CLK_CFG, 0, 3, 2);
- parent_rate = ulClkGetRate(CLK_XTAL24M);
- div = DIV_ROUND_UP(parent_rate, freq);
- vSysctlConfigure(SYS_VOU_CLK_CFG, 3, 0x1f, div - 1);
- return parent_rate / div;
- } else { //14-16MH配置为16MHZ
- vClkSetRate(CLK_LCD, 16000000);
- return ulClkGetRate(CLK_LCD);
- }
- }
- static int ark_lcd_clk_init(uint32_t freq)
- {
- if (freq >= 16000000) {
- vClkSetRate(CLK_LCD, freq);
- printf("%s, set clk:%d\n", __func__, ulClkGetRate(CLK_LCD));
- } else {
- uint32_t real_freq = ark_lcd_set_clk_below16(freq);
- printf("%s, set clk:%d\n", __func__, real_freq);
- }
- return 0;
- }
- #ifdef AWTK
- #include <assert.h>
- static volatile fb_queue_s *last_fb = NULL;
- #endif
- static void ark_lcd_interrupt(void *param)
- {
- struct ark_lcd_data *lcd = (struct ark_lcd_data *)param;
- uint32_t status = readl(REGS_LCD_BASE+LCD_STATUS_REG);
- writel(0x07, REGS_LCD_BASE+LCD_INTR_CLR);
- //frame vsync
- #ifdef AWTK
- //frame vsync
- if( status & (0x01 << 0) ) // Lcd_done_intr LCD场结束中断状态位
- {
- // 进入消隐区, 切换framebuffer
- //unsigned int fb_curr_address = (*(volatile unsigned *)(REGS_LCD_BASE + 0x28c)); // layer 1 当前显示的基址
- //fb_queue_s *next_ready_unit = fb_queue_get_ready_unit ();
- fb_queue_s *next_ready_unit = fb_queue_get_ready_unit_isr ();
- if(next_ready_unit)
- {
- fb_queue_s *fb_temp;
- //(*(volatile unsigned *)(REGS_LCD_BASE + 31 * 4)) = next_ready_unit->fb_base;
- //*(volatile unsigned int *)(REGS_LCD_BASE + 0x90) |= 0x02;
- fb_temp = (fb_queue_s *)last_fb;
- last_fb = next_ready_unit;
- ark_lcd_set_osd_yaddr(LCD_UI_LAYER, (unsigned int)next_ready_unit->fb_base);
- ark_lcd_set_osd_sync(LCD_UI_LAYER);
- // 释放老的
- fb_queue_s *old_fb = fb_temp;//fb_queue_get_unit_from_base (fb_curr_address);
- if(old_fb)
- {
- //fb_queue_set_free (old_fb);
- fb_queue_set_free_isr (old_fb);
- }
- else
- {
- static int fb_error_count = 0;
- fb_error_count ++;
- if(fb_error_count > 10)
- assert (0);
- }
- //XM_printf ("o %x, n %x\n", fb_curr_address, next_ready_unit->fb_base);
- }
- }
- #endif
- if (status & (1<<0)) {
- int i;
- for (i = 0; i < LCD_OSD_NUMS; i++)
- g_osdConfig[i] = 0;
- if (lcd->waitbits)
- xEventGroupSetBitsFromISR(lcd->vsync_waitq, lcd->waitbits, NULL);
- }
- }
- #if LCD_INTERFACE_TYPE == LCD_INTERFACE_LVDS
- static void lvds_screen_reset(void)
- {
- gpio_direction_output(LVDS_SCREEN_RST_GPIO, 0);
- udelay(100);
- gpio_direction_output(LVDS_SCREEN_RST_GPIO, 1);
- mdelay(20);
- }
- #endif
- int lcd_init(void)
- {
- int ret = ENOERR;
- struct ark_lcd_data *lcd = (struct ark_lcd_data *)pvPortMalloc(sizeof(struct ark_lcd_data));
- if(!lcd) {
- printf("ERR: %s, malloc failed\n", __func__);
- return -ENOMEM;
- }
- memset(lcd, 0, sizeof(struct ark_lcd_data));
- fb_buf = pvPortMalloc(FB_SIZE * FB_COUNT);
- if (!fb_buf) {
- printf("ERR: malloc framebuffer fail.\n");
- vPortFree(lcd);
- return -ENOMEM;
- }
- fb_addr = (uint32_t)fb_buf;
- sys_soft_reset(softreset_lcd);
- /* lcd timing init */
- lcd->timing.vs_start = 0;
- lcd->timing.hs_start = 0;
- lcd->timing.hd = LCD_WIDTH;
- lcd->timing.vd = LCD_HEIGHT;
- lcd->timing.vbp = LCD_TIMING_VBP;
- lcd->timing.vfp = LCD_TIMING_VFP;
- lcd->timing.vsw = LCD_TIMING_VSW;
- lcd->timing.hbp = LCD_TIMING_HBP;
- lcd->timing.hfp = LCD_TIMING_HFP;
- lcd->timing.hsw = LCD_TIMING_HSW;
- ark_lcd_clk_init(LCD_CLK_FREQ);
- #if LCD_INTERFACE_TYPE == LCD_INTERFACE_LVDS
- uint32_t lvds_cfg = 0xe0ec;
- #if LVDS_PANEL_FORMAT == LVDS_PANEL_FORMAT_TI
- lvds_cfg |= (1 << 16);
- #endif
- #if LVDS_PANEL_DATA == LVDS_PANEL_DATA_8BIT
- lvds_cfg &= ~(1 << 15);
- lvds_cfg |= (1 << 4) | 1;
- #endif
- writel(lvds_cfg, REGS_SYSCTL_BASE + SYS_ANA3_CFG);
- lvds_screen_reset();
- #endif
- ark_lcd_timing_init(lcd);
- lcd->vsync_waitq = xEventGroupCreate();
- ark_lcd_hw_init();
- #ifdef AWTK
- fb_queue_init ();
- #endif
- request_irq(LCD_IRQn, 0, ark_lcd_interrupt, lcd);
- g_lcd = lcd;
- return ret;
- }
- void lcd_uninit(void)
- {
- if(g_lcd) {
- vPortFree(g_lcd);
- g_lcd = NULL;
- }
- }
|