drv_vpost.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /**************************************************************************//**
  2. *
  3. * @copyright (C) 2020 Nuvoton Technology Corp. All rights reserved.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Change Logs:
  8. * Date Author Notes
  9. * 2021-4-13 Wayne First version
  10. *
  11. ******************************************************************************/
  12. #include <rtconfig.h>
  13. #if defined(BSP_USING_VPOST)
  14. #include <rthw.h>
  15. #include <rtdevice.h>
  16. #include <rtdbg.h>
  17. #include "NuMicro.h"
  18. #include <drv_sys.h>
  19. /* Private typedef --------------------------------------------------------------*/
  20. #define DEF_VPOST_BUFFER_NUMBER 3
  21. typedef enum
  22. {
  23. eVpost_LCD,
  24. #if defined(BSP_USING_VPOST_OSD)
  25. eVpost_OSD,
  26. #endif
  27. eVpost_Cnt
  28. } E_VPOST_LAYER;
  29. struct nu_vpost
  30. {
  31. struct rt_device dev;
  32. char *name;
  33. E_VPOST_LAYER layer;
  34. IRQn_Type irqn;
  35. E_SYS_IPRST rstidx;
  36. E_SYS_IPCLK clkidx;
  37. uint32_t last_commit;
  38. struct rt_device_graphic_info info;
  39. };
  40. typedef struct nu_vpost *nu_vpost_t;
  41. static volatile uint32_t s_u32VSyncBlank = 0;
  42. static volatile uint32_t s_u32UnderRun = 0;
  43. static struct rt_completion vsync_wq;
  44. static struct nu_vpost nu_fbdev[eVpost_Cnt] =
  45. {
  46. {
  47. .name = "lcd",
  48. .layer = eVpost_LCD,
  49. .irqn = IRQ_LCD,
  50. .rstidx = LCDRST,
  51. .clkidx = LCDCKEN,
  52. }
  53. #if defined(BSP_USING_VPOST_OSD)
  54. , {
  55. .name = "osd",
  56. .layer = eVpost_OSD,
  57. .irqn = (IRQn_Type) - 1,
  58. .rstidx = SYS_IPRST_NA,
  59. .clkidx = SYS_IPCLK_NA,
  60. }
  61. #endif
  62. };
  63. rt_weak void nu_lcd_backlight_on(void) { }
  64. rt_weak void nu_lcd_backlight_off(void) { }
  65. static rt_err_t vpost_layer_open(rt_device_t dev, rt_uint16_t oflag)
  66. {
  67. nu_vpost_t psVpost = (nu_vpost_t)dev;
  68. RT_ASSERT(psVpost != RT_NULL);
  69. switch (psVpost->layer)
  70. {
  71. case eVpost_LCD:
  72. vpostVAStartTrigger();
  73. break;
  74. #if defined(BSP_USING_VPOST_OSD)
  75. case eVpost_OSD:
  76. vpostVAStartTrigger();
  77. /* Set scale to 1:1 */
  78. vpostOSDScalingCtrl(1, 0, 0);
  79. #if (BSP_LCD_BPP==32)
  80. vpostOSDSetColMask(0xff, 0xff, 0xff);
  81. #else
  82. vpostOSDSetColMask(0x1f, 0x3f, 0x1f);
  83. #endif
  84. /* Enable color key function */
  85. vpostOSDSetColKey(0, 0, 0);
  86. /* Configure overlay function of OSD to display OSD image */
  87. vpostOSDSetOverlay(DISPLAY_OSD, DISPLAY_OSD, 0);
  88. vpostOSDEnable();
  89. break;
  90. #endif
  91. default:
  92. return -RT_ERROR;
  93. }
  94. return RT_EOK;
  95. }
  96. static rt_err_t vpost_layer_close(rt_device_t dev)
  97. {
  98. nu_vpost_t psVpost = (nu_vpost_t)dev;
  99. RT_ASSERT(psVpost != RT_NULL);
  100. switch (psVpost->layer)
  101. {
  102. case eVpost_LCD:
  103. #if defined(BSP_USING_VPOST_OSD)
  104. if (nu_fbdev[eVpost_OSD].dev.ref_count == 0)
  105. #endif
  106. vpostVAStopTrigger();
  107. break;
  108. #if defined(BSP_USING_VPOST_OSD)
  109. case eVpost_OSD:
  110. vpostOSDDisable();
  111. if (nu_fbdev[eVpost_LCD].dev.ref_count == 0)
  112. {
  113. /* Also stop displaying */
  114. vpostVAStopTrigger();
  115. }
  116. break;
  117. #endif
  118. default:
  119. return -RT_ERROR;
  120. }
  121. return RT_EOK;
  122. }
  123. static rt_err_t vpost_layer_control(rt_device_t dev, int cmd, void *args)
  124. {
  125. nu_vpost_t psVpost = (nu_vpost_t)dev;
  126. RT_ASSERT(psVpost != RT_NULL);
  127. switch (cmd)
  128. {
  129. case RTGRAPHIC_CTRL_POWERON:
  130. {
  131. nu_lcd_backlight_on();
  132. }
  133. break;
  134. case RTGRAPHIC_CTRL_POWEROFF:
  135. {
  136. nu_lcd_backlight_off();
  137. }
  138. break;
  139. case RTGRAPHIC_CTRL_GET_INFO:
  140. {
  141. struct rt_device_graphic_info *info = (struct rt_device_graphic_info *) args;
  142. RT_ASSERT(info != RT_NULL);
  143. rt_memcpy(args, (void *)&psVpost->info, sizeof(struct rt_device_graphic_info));
  144. }
  145. break;
  146. case RTGRAPHIC_CTRL_PAN_DISPLAY:
  147. {
  148. if (args != RT_NULL)
  149. {
  150. uint8_t *pu8BufPtr = (uint8_t *)args;
  151. psVpost->last_commit = s_u32VSyncBlank;
  152. /* Pan display */
  153. switch (psVpost->layer)
  154. {
  155. case eVpost_LCD:
  156. vpostSetFrameBuffer(pu8BufPtr);
  157. break;
  158. #if defined(BSP_USING_VPOST_OSD)
  159. case eVpost_OSD:
  160. vpostSetOSDBuffer(pu8BufPtr);
  161. break;
  162. #endif
  163. default:
  164. return -RT_ERROR;
  165. }
  166. }
  167. else
  168. return -RT_ERROR;
  169. }
  170. break;
  171. case RTGRAPHIC_CTRL_WAIT_VSYNC:
  172. {
  173. if (args != RT_NULL)
  174. psVpost->last_commit = s_u32VSyncBlank + 1;
  175. if (psVpost->last_commit >= s_u32VSyncBlank)
  176. {
  177. rt_completion_init(&vsync_wq);
  178. rt_completion_wait(&vsync_wq, RT_TICK_PER_SECOND / 60);
  179. }
  180. }
  181. break;
  182. default:
  183. return -RT_ERROR;
  184. }
  185. return RT_EOK;
  186. }
  187. static rt_err_t vpost_layer_init(rt_device_t dev)
  188. {
  189. nu_vpost_t psVpost = (nu_vpost_t)dev;
  190. RT_ASSERT(psVpost != RT_NULL);
  191. /* Enable VPOST engine clock. */
  192. nu_sys_ipclk_enable(LCDCKEN);
  193. rt_completion_init(&vsync_wq);
  194. outpw(REG_LCM_INT_CS, VPOSTB_UNDERRUN_EN | VPOSTB_DISP_F_EN);
  195. outpw(REG_LCM_DCCS, (inpw(REG_LCM_DCCS) | (1 << 4)));
  196. return RT_EOK;
  197. }
  198. static void nu_vpost_calculate_fps(void)
  199. {
  200. #define DEF_PERIOD_SEC 10
  201. static uint32_t u32LastTick = 0;
  202. static uint32_t u32VSyncBlank = 0;
  203. static uint32_t u32UnderRun = 0;
  204. uint32_t u32CurrTick = rt_tick_get();
  205. if ((u32CurrTick - u32LastTick) > (DEF_PERIOD_SEC * RT_TICK_PER_SECOND))
  206. {
  207. rt_kprintf("VPOST: %d FPS, URPS: %d\n",
  208. (s_u32VSyncBlank - u32VSyncBlank) / DEF_PERIOD_SEC,
  209. (s_u32UnderRun - u32UnderRun) / DEF_PERIOD_SEC);
  210. u32LastTick = u32CurrTick;
  211. u32VSyncBlank = s_u32VSyncBlank;
  212. u32UnderRun = s_u32UnderRun;
  213. }
  214. }
  215. static void nu_vpost_isr(int vector, void *param)
  216. {
  217. /*
  218. #define VPOSTB_DISP_F_INT ((UINT32)1<<31)
  219. #define VPOSTB_DISP_F_STATUS (1<<30)
  220. #define VPOSTB_UNDERRUN_INT (1<<29)
  221. #define VPOSTB_BUS_ERROR_INT (1<<28)
  222. #define VPOSTB_FLY_ERR (1<<27)
  223. #define VPOSTB_UNDERRUN_EN (1<<1)
  224. #define VPOSTB_DISP_F_EN (1)
  225. */
  226. uint32_t u32VpostIRQStatus = inpw(REG_LCM_INT_CS);
  227. if (u32VpostIRQStatus & VPOSTB_DISP_F_STATUS)
  228. {
  229. outpw(REG_LCM_INT_CS, inpw(REG_LCM_INT_CS) | VPOSTB_DISP_F_STATUS);
  230. s_u32VSyncBlank++;
  231. rt_completion_done(&vsync_wq);
  232. }
  233. else if (u32VpostIRQStatus & VPOSTB_UNDERRUN_INT)
  234. {
  235. s_u32UnderRun++;
  236. outpw(REG_LCM_INT_CS, inpw(REG_LCM_INT_CS) | VPOSTB_UNDERRUN_INT);
  237. }
  238. else if (u32VpostIRQStatus & VPOSTB_BUS_ERROR_INT)
  239. {
  240. outpw(REG_LCM_INT_CS, inpw(REG_LCM_INT_CS) | VPOSTB_BUS_ERROR_INT);
  241. }
  242. nu_vpost_calculate_fps();
  243. }
  244. int rt_hw_vpost_init(void)
  245. {
  246. int i = -1;
  247. rt_err_t ret;
  248. VPOST_T *psVpostLcmInst = vpostLCMGetInstance(VPOST_USING_LCD_IDX);
  249. RT_ASSERT(psVpostLcmInst != RT_NULL);
  250. if ((psVpostLcmInst->u32DevWidth * psVpostLcmInst->u32DevHeight) > (480 * 272))
  251. {
  252. /* LCD clock is selected from UPLL and divide to 20MHz */
  253. outpw(REG_CLK_DIVCTL1, (inpw(REG_CLK_DIVCTL1) & ~0xff1f) | 0xE18);
  254. /* LCD clock is selected from UPLL and divide to 30MHz */
  255. //outpw(REG_CLK_DIVCTL1, (inpw(REG_CLK_DIVCTL1) & ~0xff1f) | 0x918);
  256. /* LCD clock is selected from UPLL and divide to 33.3MHz */
  257. //outpw(REG_CLK_DIVCTL1, (inpw(REG_CLK_DIVCTL1) & ~0xff1f) | 0x818);
  258. }
  259. else
  260. {
  261. /* LCD clock is selected from UPLL and divide to 10MHz */
  262. outpw(REG_CLK_DIVCTL1, (inpw(REG_CLK_DIVCTL1) & ~0xff1f) | 0xE19);
  263. }
  264. /* Initial LCM */
  265. vpostLCMInit(VPOST_USING_LCD_IDX);
  266. /* Set scale to 1:1 */
  267. vpostVAScalingCtrl(1, 0, 1, 0, VA_SCALE_INTERPOLATION);
  268. for (i = eVpost_LCD; i < eVpost_Cnt; i++)
  269. {
  270. nu_vpost_t psVpost = &nu_fbdev[i];
  271. rt_memset((void *)&psVpost->info, 0, sizeof(struct rt_device_graphic_info));
  272. /* Register VPOST information */
  273. psVpost->info.bits_per_pixel = BSP_LCD_BPP;
  274. psVpost->info.pixel_format = (BSP_LCD_BPP == 32) ? RTGRAPHIC_PIXEL_FORMAT_ARGB888 : RTGRAPHIC_PIXEL_FORMAT_RGB565;
  275. psVpost->info.pitch = psVpostLcmInst->u32DevWidth * (BSP_LCD_BPP / 8);
  276. psVpost->info.width = psVpostLcmInst->u32DevWidth;
  277. psVpost->info.height = psVpostLcmInst->u32DevHeight;
  278. /* Get pointer of video frame buffer */
  279. /* Set display color depth */
  280. /* Note: before get pointer of frame buffer, must set display color depth first */
  281. if (psVpost->layer == eVpost_LCD)
  282. {
  283. #if (BSP_LCD_BPP==32)
  284. vpostSetVASrc(VA_SRC_RGB888);
  285. #else
  286. vpostSetVASrc(VA_SRC_RGB565);
  287. #endif
  288. psVpost->info.framebuffer = (rt_uint8_t *)vpostGetMultiFrameBuffer(DEF_VPOST_BUFFER_NUMBER);
  289. }
  290. #if defined(BSP_USING_VPOST_OSD)
  291. else if (psVpost->layer == eVpost_OSD)
  292. {
  293. vpostOSDSetWindow(0, 0, psVpost->info.width, psVpost->info.height);
  294. #if (BSP_LCD_BPP==32)
  295. vpostSetOSDSrc(OSD_SRC_RGB888);
  296. #else
  297. vpostSetOSDSrc(OSD_SRC_RGB565);
  298. #endif
  299. psVpost->info.framebuffer = (rt_uint8_t *)vpostGetMultiOSDBuffer(DEF_VPOST_BUFFER_NUMBER);
  300. }
  301. #endif
  302. if (psVpost->info.framebuffer == NULL)
  303. {
  304. rt_kprintf("Fail to get VRAM buffer.\n");
  305. RT_ASSERT(0);
  306. }
  307. else
  308. {
  309. uint32_t u32FBSize = psVpost->info.pitch * psVpostLcmInst->u32DevHeight;
  310. psVpost->info.smem_len = u32FBSize * DEF_VPOST_BUFFER_NUMBER;
  311. rt_memset(psVpost->info.framebuffer, 0, u32FBSize);
  312. }
  313. /* Register member functions of lcd device */
  314. psVpost->dev.type = RT_Device_Class_Graphic;
  315. psVpost->dev.init = vpost_layer_init;
  316. psVpost->dev.open = vpost_layer_open;
  317. psVpost->dev.close = vpost_layer_close;
  318. psVpost->dev.control = vpost_layer_control;
  319. /* Register graphic device driver */
  320. ret = rt_device_register(&psVpost->dev, psVpost->name, RT_DEVICE_FLAG_RDWR);
  321. RT_ASSERT(ret == RT_EOK);
  322. if (psVpost->layer == eVpost_LCD)
  323. {
  324. rt_hw_interrupt_install(psVpost->irqn, nu_vpost_isr, psVpost, psVpost->name);
  325. rt_hw_interrupt_umask(psVpost->irqn);
  326. }
  327. rt_kprintf("%s's fbmem at 0x%08x.\n", psVpost->name, psVpost->info.framebuffer);
  328. }
  329. /* For saving memory bandwidth. */
  330. vpostLCMDeinit();
  331. return (int)ret;
  332. }
  333. INIT_DEVICE_EXPORT(rt_hw_vpost_init);
  334. /* Support "vpost_set_osd_colkey" command line in msh mode */
  335. static rt_err_t vpost_set_osd_colkey(int argc, char **argv)
  336. {
  337. rt_uint32_t index, len, arg[4];
  338. rt_memset(arg, 0, sizeof(arg));
  339. len = (argc >= 4) ? 4 : argc;
  340. for (index = 0; index < (len - 1); index ++)
  341. {
  342. arg[index] = atol(argv[index + 1]);
  343. }
  344. /* Enable color key function */
  345. vpostOSDSetColKey(arg[0], arg[1], arg[2]);
  346. /* Configure overlay function of OSD to display VIDEO image */
  347. vpostOSDSetOverlay(DISPLAY_VIDEO, DISPLAY_OSD, 0);
  348. return 0;
  349. }
  350. MSH_CMD_EXPORT(vpost_set_osd_colkey, e.g: vpost_set_osd_colkey R G B);
  351. /* Support "vpost_show_layer" command line in msh mode */
  352. static rt_err_t vpost_show_layer(int argc, char **argv)
  353. {
  354. rt_uint32_t index, len, arg[2];
  355. nu_vpost_t psVpostLayer;
  356. rt_memset(arg, 0, sizeof(arg));
  357. len = (argc >= 2) ? 2 : argc;
  358. for (index = 0; index < (len - 1); index ++)
  359. {
  360. arg[index] = atol(argv[index + 1]);
  361. }
  362. psVpostLayer = &nu_fbdev[arg[0]];
  363. return rt_device_open(&psVpostLayer->dev, RT_DEVICE_FLAG_RDWR);
  364. }
  365. MSH_CMD_EXPORT(vpost_show_layer, e.g: vpost_show_layer layer);
  366. /* Support "vpost_hide_layer" command line in msh mode */
  367. static rt_err_t vpost_hide_layer(int argc, char **argv)
  368. {
  369. rt_uint32_t index, len, arg[2];
  370. nu_vpost_t psVpostLayer;
  371. rt_memset(arg, 0, sizeof(arg));
  372. len = (argc >= 2) ? 2 : argc;
  373. for (index = 0; index < (len - 1); index ++)
  374. {
  375. arg[index] = atol(argv[index + 1]);
  376. }
  377. psVpostLayer = &nu_fbdev[arg[0]];
  378. return rt_device_close(&psVpostLayer->dev);
  379. }
  380. MSH_CMD_EXPORT(vpost_hide_layer, e.g: vpost_hide_layer layer);
  381. /* Support "vpost_fill_color" command line in msh mode */
  382. static rt_err_t vpost_fill_color(int argc, char **argv)
  383. {
  384. rt_uint32_t index, len, arg[5];
  385. nu_vpost_t psVpostLayer;
  386. rt_memset(arg, 0, sizeof(arg));
  387. len = (argc >= 5) ? 5 : argc;
  388. for (index = 0; index < (len - 1); index ++)
  389. {
  390. arg[index] = atol(argv[index + 1]);
  391. }
  392. psVpostLayer = &nu_fbdev[arg[0]];
  393. if (psVpostLayer->info.framebuffer != RT_NULL)
  394. {
  395. int i;
  396. uint32_t fill_num = psVpostLayer->info.height * psVpostLayer->info.width;
  397. uint32_t *fbmem_start = (uint32_t *)psVpostLayer->info.framebuffer;
  398. uint32_t color = (arg[1] << 16) | (arg[2] << 8) | arg[3] ;
  399. for (i = 0; i < fill_num; i++)
  400. {
  401. rt_memcpy((void *)&fbmem_start[i], &color, (psVpostLayer->info.bits_per_pixel / 8));
  402. }
  403. }
  404. return 0;
  405. }
  406. MSH_CMD_EXPORT(vpost_fill_color, e.g: vpost_fill_color layer R G B);
  407. #endif /* if defined(BSP_USING_VPOST) */