drv_dc.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Email: opensource_embedded@phytium.com.cn
  7. *
  8. * Change Logs:
  9. * Date Author Notes
  10. * 2023-11-07 wangzongqiang first version
  11. *
  12. */
  13. #include <string.h>
  14. #include "rtconfig.h"
  15. #include <rtthread.h>
  16. #include <rtdevice.h>
  17. #define LOG_TAG "dc_drv"
  18. #include "drv_log.h"
  19. #include "drv_dc.h"
  20. #include "mm_aspace.h"
  21. #include "fparameters.h"
  22. #include "fdcdp.h"
  23. #include "fdc.h"
  24. #include "fdp_hw.h"
  25. #ifdef RT_USING_SMART
  26. #include "ioremap.h"
  27. #endif
  28. static rt_uint8_t _rt_framebuffer[1024 * 768 * 4] __aligned(128) = {0};
  29. static struct rt_device_graphic_info _dc_info;
  30. typedef struct
  31. {
  32. u32 bit_depth;
  33. u32 bpc;
  34. u32 color_depth;
  35. u32 clock_mode;
  36. u32 color_rep;
  37. u32 width;
  38. u32 height;
  39. } FuserCfg;
  40. static const FuserCfg user_cfg = {
  41. .bit_depth = 8,
  42. .bpc = 8,
  43. .color_depth = 32,
  44. .clock_mode = 1,
  45. .color_rep = 0,
  46. .width = 640,
  47. .height = 480
  48. };
  49. void rt_hw_dc_register(struct phytium_dc_bus *dc_control_bus, const char *name, rt_uint32_t flag, void *data);
  50. static rt_err_t dc_config(struct phytium_dc_bus *dc_control_bus)
  51. {
  52. RT_ASSERT(dc_control_bus);
  53. rt_uint32_t chan = dc_control_bus->fdc_id;
  54. FDcDp *instance_p = &dc_control_bus->dc_handle;
  55. return RT_EOK;
  56. }
  57. static rt_err_t rt_dc_init(struct phytium_dc_bus *device)
  58. {
  59. RT_ASSERT(device != RT_NULL);
  60. rt_err_t ret;
  61. FDcDp *instance_p = &device->dc_handle;
  62. rt_uint32_t chan = device->fdc_id;
  63. FDcDpCfgInitialize(instance_p, chan);
  64. instance_p->dc_instance_p[chan].config = *FDcLookupConfig(chan);
  65. instance_p->dp_instance_p[chan].config = *FDpLookupConfig(chan);
  66. instance_p->dc_instance_p[chan].crtc.bpc = user_cfg.bpc;
  67. instance_p->dc_instance_p[chan].color_depth = user_cfg.color_depth;
  68. instance_p->dc_instance_p[chan].channel = chan;
  69. instance_p->dc_instance_p[chan].fb_addr = (uintptr)_rt_framebuffer;/*当前例程虚拟地址和物理地址一致,实际需要根据需要进行映射*/
  70. instance_p->dc_instance_p[chan].fb_virtual = (uintptr)_rt_framebuffer;
  71. instance_p->dp_instance_p[chan].trans_config.clock_mode = user_cfg.clock_mode;
  72. instance_p->dp_instance_p[chan].trans_config.color_rep_format = user_cfg.color_rep;
  73. instance_p->dp_instance_p[chan].trans_config.bit_depth = user_cfg.bit_depth;
  74. FDcDpGeneralCfgInitial(instance_p, chan);
  75. _dc_info.framebuffer = (rt_uint8_t *)instance_p->dc_instance_p[chan].fb_addr;
  76. _dc_info.bits_per_pixel = DISPLAY_COLOR_DEPTH;
  77. _dc_info.pixel_format = RTGRAPHIC_PIXEL_FORMAT_RGB565P;
  78. _dc_info.width = FB_XSIZE;
  79. _dc_info.height = FB_YSIZE;
  80. rt_hw_dc_register(device, device->name, RT_DEVICE_FLAG_RDWR, NULL);
  81. dc_config(device);
  82. ret = FDcDpInitial(instance_p, device->fdc_id, user_cfg.width, user_cfg.height);
  83. if (ret != RT_EOK)
  84. {
  85. LOG_E("Init dc failed, ret: 0x%x", ret);
  86. return -RT_ERROR;;
  87. }
  88. return RT_EOK;
  89. }
  90. static rt_err_t rt_dc_control(rt_device_t dev, int cmd, void *args)
  91. {
  92. RT_ASSERT(dev);
  93. struct phytium_dc_bus *dc_bus;
  94. dc_bus = (struct phytium_dc_bus *)(dev);
  95. switch (cmd)
  96. {
  97. case RTGRAPHIC_CTRL_RECT_UPDATE:
  98. break;
  99. case RTGRAPHIC_CTRL_POWERON:
  100. break;
  101. case RTGRAPHIC_CTRL_POWEROFF:
  102. break;
  103. case RTGRAPHIC_CTRL_GET_INFO:
  104. rt_memcpy(args, &_dc_info, sizeof(_dc_info));
  105. break;
  106. case RTGRAPHIC_CTRL_SET_MODE:
  107. break;
  108. }
  109. return RT_EOK;
  110. }
  111. #ifdef RT_USING_DEVICE_OPS
  112. const static struct rt_device_ops dc_ops =
  113. {
  114. rt_dc_init,
  115. RT_NULL,
  116. RT_NULL,
  117. RT_NULL,
  118. RT_NULL,
  119. rt_dc_control
  120. };
  121. #endif
  122. void rt_hw_dc_register(struct phytium_dc_bus *dc_control_bus, const char *name, rt_uint32_t flag, void *data)
  123. {
  124. RT_ASSERT(dc_control_bus != RT_NULL);
  125. struct rt_device *dc;
  126. dc = &(dc_control_bus->parent);
  127. dc->type = RT_Device_Class_Graphic;
  128. #ifdef RT_USING_DEVICE_OPS
  129. dc->ops = &dc_ops;
  130. #else
  131. dc->init = rt_dc_init;
  132. dc->control = rt_dc_control;
  133. #endif
  134. dc->user_data = data;
  135. /* register Display Controller device to RT-Thread */
  136. rt_device_register(dc, name, RT_DEVICE_FLAG_RDWR);
  137. }
  138. #if defined(RT_USING_DC_CHANNEL0)
  139. static struct phytium_dc_bus dev_dc0;
  140. #endif
  141. #if defined(RT_USING_DC_CHANNEL1)
  142. static struct phytium_dc_bus dev_dc1;
  143. #endif
  144. int rt_hw_dc_init(void)
  145. {
  146. #if defined(RT_USING_DC_CHANNEL0)
  147. dev_dc0.name = "DC0";
  148. dev_dc0.fdc_id = FDCDP_ID0;
  149. rt_dc_init(&dev_dc0);
  150. #endif
  151. #if defined(RT_USING_DC_CHANNEL1)
  152. dev_dc1.name = "DC1";
  153. dev_dc1.fdc_id = FDCDP_ID1;
  154. rt_dc_init(&dev_dc1);
  155. #endif
  156. return RT_EOK;
  157. }
  158. INIT_DEVICE_EXPORT(rt_hw_dc_init);