drv_fb.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * Copyright (c) 2006-2019, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2019-08-29 zdzn first version
  9. */
  10. #include <rthw.h>
  11. #include <stdint.h>
  12. #include <rtthread.h>
  13. #include "mbox.h"
  14. #include "drv_fb.h"
  15. #include "mmu.h"
  16. #define LCD_WIDTH (640)
  17. #define LCD_HEIGHT (480)
  18. #define LCD_DEPTH (32)
  19. #define TAG_ALLOCATE_BUFFER 0x00040001
  20. #define TAG_SET_PHYS_WIDTH_HEIGHT 0x00048003
  21. #define TAG_SET_VIRT_WIDTH_HEIGHT 0x00048004
  22. #define TAG_SET_DEPTH 0x00048005
  23. #define TAG_SET_PIXEL_ORDER 0x00048006
  24. #define TAG_GET_PITCH 0x00040008
  25. #define TAG_SET_VIRT_OFFSET 0x00048009
  26. #define TAG_END 0x00000000
  27. #define LCD_DEVICE(dev) (struct rt_hdmi_fb_device*)(dev)
  28. static struct rt_hdmi_fb_device _hdmi;
  29. typedef rt_uint16_t color_t;
  30. rt_err_t hdmi_fb_open(rt_device_t dev, rt_uint16_t oflag)
  31. {
  32. return RT_EOK;
  33. }
  34. rt_err_t hdmi_fb_close(rt_device_t dev)
  35. {
  36. return RT_EOK;
  37. }
  38. rt_size_t hdmi_fb_read(rt_device_t dev, rt_off_t pos, void *buf, rt_size_t size)
  39. {
  40. return 0;
  41. }
  42. rt_size_t hdmi_fb_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
  43. {
  44. return size;
  45. }
  46. rt_err_t hdmi_fb_control(rt_device_t dev, int cmd, void *args)
  47. {
  48. struct rt_hdmi_fb_device *lcd = LCD_DEVICE(dev);
  49. switch (cmd)
  50. {
  51. case RTGRAPHIC_CTRL_RECT_UPDATE:
  52. {
  53. struct rt_device_rect_info *info = (struct rt_device_rect_info*)args;
  54. info = info;
  55. }
  56. break;
  57. case RTGRAPHIC_CTRL_GET_INFO:
  58. {
  59. struct rt_device_graphic_info* info = (struct rt_device_graphic_info*)args;
  60. RT_ASSERT(info != RT_NULL);
  61. info->pixel_format = RTGRAPHIC_PIXEL_FORMAT_RGB888;
  62. info->bits_per_pixel= LCD_DEPTH;
  63. info->width = lcd->width;
  64. info->height = lcd->height;
  65. info->framebuffer = lcd->fb;//(rt_uint8_t *)lcd->fb;
  66. }
  67. break;
  68. }
  69. return RT_EOK;
  70. }
  71. const static struct rt_device_ops hdmi_fb_ops =
  72. {
  73. RT_NULL,
  74. hdmi_fb_open,
  75. hdmi_fb_close,
  76. hdmi_fb_read,
  77. hdmi_fb_write,
  78. hdmi_fb_control
  79. };
  80. rt_err_t rt_hdmi_fb_device_init(struct rt_hdmi_fb_device *hdmi_fb, const char *name)
  81. {
  82. struct rt_device *device;
  83. RT_ASSERT(hdmi_fb != RT_NULL);
  84. device = &hdmi_fb->parent;
  85. /* set device type */
  86. device->type = RT_Device_Class_Graphic;
  87. /* initialize device interface */
  88. #ifdef RT_USING_DEVICE_OPS
  89. device->ops = &hdmi_fb_ops;
  90. #else
  91. device->init = RT_NULL;
  92. device->open = hdmi_fb_open;
  93. device->close = hdmi_fb_close;
  94. device->read = hdmi_fb_read;
  95. device->write = hdmi_fb_write;
  96. device->control = hdmi_fb_control;
  97. #endif
  98. /* register to device manager */
  99. rt_device_register(device, name, RT_DEVICE_FLAG_RDWR);
  100. return RT_EOK;
  101. }
  102. int hdmi_fb_init(void)
  103. {
  104. mbox[0] = 4 * 35;
  105. mbox[1] = MBOX_REQUEST;
  106. mbox[2] = TAG_ALLOCATE_BUFFER;//get framebuffer, gets alignment on request
  107. mbox[3] = 8;
  108. mbox[4] = 0;
  109. mbox[5] = 4096; //FrameBufferInfo.pointer
  110. mbox[6] = 0; //FrameBufferInfo.size
  111. mbox[7] = TAG_SET_PHYS_WIDTH_HEIGHT;
  112. mbox[8] = 8;
  113. mbox[9] = 0;
  114. mbox[10] = LCD_WIDTH;
  115. mbox[11] = LCD_HEIGHT;
  116. mbox[12] = TAG_SET_VIRT_WIDTH_HEIGHT;
  117. mbox[13] = 8;
  118. mbox[14] = 0;
  119. mbox[15] = LCD_WIDTH;
  120. mbox[16] = LCD_HEIGHT;
  121. mbox[17] = TAG_SET_DEPTH;
  122. mbox[18] = 4;
  123. mbox[19] = 0;
  124. mbox[20] = 16; //FrameBufferInfo.depth RGB 565
  125. mbox[21] = TAG_SET_PIXEL_ORDER;
  126. mbox[22] = 4;
  127. mbox[23] = 0;
  128. mbox[24] = 1; //RGB, not BGR preferably
  129. mbox[25] = TAG_GET_PITCH;
  130. mbox[26] = 4;
  131. mbox[27] = 0;
  132. mbox[28] = 0;
  133. mbox[29] = TAG_SET_VIRT_OFFSET;
  134. mbox[30] = 8;
  135. mbox[31] = 8;
  136. mbox[32] = 0;
  137. mbox[33] = 0;
  138. mbox[34] = TAG_END;
  139. mbox_call(MBOX_CH_PROP, MMU_DISABLE);
  140. _hdmi.fb = (rt_uint8_t *)(uintptr_t)(mbox[5] & 0x3FFFFFFF);
  141. _hdmi.width = LCD_WIDTH;
  142. _hdmi.height = LCD_HEIGHT;
  143. _hdmi.depth = LCD_DEPTH;
  144. _hdmi.pitch = 0;
  145. _hdmi.pixel_format = RTGRAPHIC_PIXEL_FORMAT_RGB888;
  146. armv8_map((unsigned long)_hdmi.fb, (unsigned long)_hdmi.fb, 0x200000, MEM_ATTR_MEMORY);
  147. rt_kprintf("_hdmi.fb is %p\n", _hdmi.fb);
  148. rt_hdmi_fb_device_init(&_hdmi, "lcd");
  149. return 0;
  150. }
  151. INIT_DEVICE_EXPORT(hdmi_fb_init);