drv_clcd.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020/12/31 Bernard Add license info
  9. */
  10. #include <stdint.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include <rtthread.h>
  14. #ifdef RT_USING_SMART
  15. #include <lwp.h>
  16. #include <lwp_user_mm.h>
  17. #endif
  18. #include <board.h>
  19. #include "drv_clcd.h"
  20. #include "rt_lcd.h"
  21. #define CLCD_WIDTH (BSP_LCD_WIDTH)
  22. #define CLCD_HEIGHT (BSP_LCD_HEIGHT)
  23. #define CLCD_DEVICE(dev) (struct drv_clcd_device*)(dev)
  24. #define PL111_CR_EN 0x001
  25. #define PL111_CR_PWR 0x800
  26. #define PL111_IOBASE 0x10020000
  27. #define PL111_PALBASE (PL111_IOBASE + 0x200)
  28. typedef struct _PL111MMIO
  29. {
  30. uint32_t volatile tim0; //0
  31. uint32_t volatile tim1; //4
  32. uint32_t volatile tim2; //8
  33. uint32_t volatile tim3; //c
  34. uint32_t volatile upbase; //10
  35. uint32_t volatile f; //14
  36. uint32_t volatile control; //18
  37. uint32_t volatile g; //1c
  38. } PL111MMIO;
  39. struct drv_clcd_device
  40. {
  41. struct rt_device parent;
  42. int width;
  43. int height;
  44. uint8_t *fb;
  45. };
  46. struct drv_clcd_device _lcd;
  47. static rt_err_t drv_clcd_init(struct rt_device *device)
  48. {
  49. struct drv_clcd_device *lcd = CLCD_DEVICE(device);
  50. (void)lcd; /* nothing, right now */
  51. return RT_EOK;
  52. }
  53. static rt_err_t drv_clcd_control(struct rt_device *device, int cmd, void *args)
  54. {
  55. struct drv_clcd_device *lcd = CLCD_DEVICE(device);
  56. switch (cmd)
  57. {
  58. case RTGRAPHIC_CTRL_RECT_UPDATE:
  59. {
  60. struct rt_device_rect_info *info = (struct rt_device_rect_info*)args;
  61. info = info; /* nothing, right now */
  62. }
  63. break;
  64. case RTGRAPHIC_CTRL_GET_INFO:
  65. {
  66. struct rt_device_graphic_info* info = (struct rt_device_graphic_info*)args;
  67. RT_ASSERT(info != RT_NULL);
  68. info->pixel_format = RTGRAPHIC_PIXEL_FORMAT_RGB565;
  69. info->bits_per_pixel= 16;
  70. info->width = lcd->width;
  71. info->height = lcd->height;
  72. info->framebuffer = lcd->fb;
  73. }
  74. break;
  75. case FBIOGET_FSCREENINFO:
  76. {
  77. #ifdef RT_USING_SMART
  78. struct fb_fix_screeninfo *info = (struct fb_fix_screeninfo *)args;
  79. strncpy(info->id, "lcd", sizeof(info->id));
  80. info->smem_len = lcd->width * lcd->height * 2;
  81. info->smem_start = (uint32_t)lwp_map_user_phy(lwp_self(), RT_NULL, lcd->fb,
  82. info->smem_len, 1);
  83. info->line_length = lcd->width * 2;
  84. #endif
  85. }
  86. break;
  87. case FBIOGET_VSCREENINFO:
  88. {
  89. struct fb_var_screeninfo *info = (struct fb_var_screeninfo *)args;
  90. info->bits_per_pixel = 16;
  91. info->xres = lcd->width;
  92. info->yres = lcd->height;
  93. }
  94. break;
  95. case FBIOGET_DISPINFO:
  96. break;
  97. }
  98. return RT_EOK;
  99. }
  100. #ifdef RT_USING_DEVICE_OPS
  101. const static struct rt_device_ops clcd_ops =
  102. {
  103. drv_clcd_init,
  104. RT_NULL,
  105. RT_NULL,
  106. RT_NULL,
  107. RT_NULL,
  108. drv_clcd_control
  109. };
  110. #endif
  111. int drv_clcd_hw_init(void)
  112. {
  113. PL111MMIO *plio;
  114. struct rt_device *device = &_lcd.parent;
  115. /* memset _lcd to zero */
  116. memset(&_lcd, 0x0, sizeof(_lcd));
  117. _lcd.width = CLCD_WIDTH;
  118. _lcd.height = CLCD_HEIGHT;
  119. rt_kprintf("try to allocate fb... | w - %d, h - %d | ", _lcd.width, _lcd.height);
  120. #ifdef RT_USING_SMART
  121. _lcd.fb = rt_pages_alloc(rt_page_bits(_lcd.width * _lcd.height * 2));
  122. #else
  123. _lcd.fb = rt_malloc(_lcd.width * _lcd.height * 2);
  124. #endif
  125. rt_kprintf("done!\n");
  126. rt_kprintf("fb => 0x%08x\n", _lcd.fb);
  127. if (_lcd.fb == NULL)
  128. {
  129. rt_kprintf("initialize frame buffer failed!\n");
  130. return -1;
  131. }
  132. memset(_lcd.fb, 0xff, _lcd.width * _lcd.height * 2);
  133. plio = (PL111MMIO*)PL111_IOBASE;
  134. #ifdef RT_USING_SMART
  135. plio = (PL111MMIO *)rt_ioremap((void*)PL111_IOBASE, 0x1000);
  136. #endif
  137. plio->tim0 = 0x3F1F3C00 | ((CLCD_WIDTH / 16 - 1) << 2);
  138. plio->tim1 = 0x080B6000 | (CLCD_HEIGHT - 1);
  139. plio->upbase = (uint32_t)_lcd.fb;
  140. #ifdef RT_USING_SMART
  141. plio->upbase += PV_OFFSET;
  142. #endif
  143. /* 16-bit 565 color */
  144. plio->control = 0x1921 | (0x6 << 1);
  145. device->type = RT_Device_Class_Graphic;
  146. #ifdef RT_USING_DEVICE_OPS
  147. device->ops = &clcd_ops;
  148. #else
  149. device->init = drv_clcd_init;
  150. device->control = drv_clcd_control;
  151. #endif
  152. rt_device_register(device, "lcd", RT_DEVICE_FLAG_RDWR);
  153. return 0;
  154. }
  155. INIT_DEVICE_EXPORT(drv_clcd_hw_init);