You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

150 lines
7.8 KiB

  1. // Copyright 2021 Paul Cotter (@gr1mr3aver)
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <wait.h>
  4. #include "qp_internal.h"
  5. #include "qp_comms.h"
  6. #include "qp_gc9a01.h"
  7. #include "qp_gc9a01_opcodes.h"
  8. #include "qp_tft_panel.h"
  9. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  10. // Driver storage
  11. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  12. tft_panel_dc_reset_painter_device_t gc9a01_drivers[GC9A01_NUM_DEVICES] = {0};
  13. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  14. // Initialization
  15. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  16. bool qp_gc9a01_init(painter_device_t device, painter_rotation_t rotation) {
  17. // A lot of these "unknown" opcodes are sourced from other OSS projects and are seemingly required for this display to function.
  18. // clang-format off
  19. const uint8_t gc9a01_init_sequence[] = {
  20. // Command, Delay, N, Data[N]
  21. GC9A01_SET_INTER_REG_ENABLE2, 0, 0,
  22. 0xEB, 0, 1, 0x14,
  23. GC9A01_SET_INTER_REG_ENABLE1, 0, 0,
  24. GC9A01_SET_INTER_REG_ENABLE2, 0, 0,
  25. 0xEB, 0, 1, 0x14,
  26. 0x84, 0, 1, 0x40,
  27. 0x85, 0, 1, 0xFF,
  28. 0x86, 0, 1, 0xFF,
  29. 0x87, 0, 1, 0xFF,
  30. 0x88, 0, 1, 0x0A,
  31. 0x89, 0, 1, 0x21,
  32. 0x8a, 0, 1, 0x00,
  33. 0x8b, 0, 1, 0x80,
  34. 0x8c, 0, 1, 0x01,
  35. 0x8d, 0, 1, 0x01,
  36. 0x8e, 0, 1, 0xFF,
  37. 0x8f, 0, 1, 0xFF,
  38. GC9A01_SET_FUNCTION_CTL, 0, 2, 0x00, 0x20,
  39. GC9A01_SET_PIX_FMT, 0, 1, 0x55,
  40. 0x90, 0, 4, 0x08, 0x08, 0x08, 0x08,
  41. 0xBD, 0, 1, 0x06,
  42. 0xBC, 0, 1, 0x00,
  43. 0xFF, 0, 3, 0x60, 0x01, 0x04,
  44. GC9A01_SET_POWER_CTL_2, 0, 1, 0x13,
  45. GC9A01_SET_POWER_CTL_3, 0, 1, 0x13,
  46. GC9A01_SET_POWER_CTL_4, 0, 1, 0x22,
  47. 0xBE, 0, 1, 0x11,
  48. 0xE1, 0, 2, 0x10, 0x0E,
  49. 0xDF, 0, 3, 0x21, 0x0C, 0x02,
  50. GC9A01_SET_GAMMA1, 0, 6, 0x45, 0x09, 0x08, 0x08, 0x26, 0x2A,
  51. GC9A01_SET_GAMMA2, 0, 6, 0x43, 0x70, 0x72, 0x36, 0x37, 0x6F,
  52. GC9A01_SET_GAMMA3, 0, 6, 0x45, 0x09, 0x08, 0x08, 0x26, 0x2A,
  53. GC9A01_SET_GAMMA4, 0, 6, 0x43, 0x70, 0x72, 0x36, 0x37, 0x6F,
  54. 0xED, 0, 2, 0x1B, 0x0B,
  55. 0xAE, 0, 1, 0x77,
  56. 0xCD, 0, 1, 0x63,
  57. 0x70, 0, 9, 0x07, 0x07, 0x04, 0x0E, 0x0F, 0x09, 0x07, 0x08, 0x03,
  58. GC9A01_SET_FRAME_RATE, 0, 1, 0x34,
  59. 0x62, 0, 12, 0x18, 0x0D, 0x71, 0xED, 0x70, 0x70, 0x18, 0x0F, 0x71, 0xEF, 0x70, 0x70,
  60. 0x63, 0, 12, 0x18, 0x11, 0x71, 0xF1, 0x70, 0x70, 0x18, 0x13, 0x71, 0xF3, 0x70, 0x70,
  61. 0x64, 0, 7, 0x28, 0x29, 0xF1, 0x01, 0xF1, 0x00, 0x07,
  62. 0x66, 0, 10, 0x3C, 0x00, 0xCD, 0x67, 0x45, 0x45, 0x10, 0x00, 0x00, 0x00,
  63. 0x67, 0, 10, 0x00, 0x3C, 0x00, 0x00, 0x00, 0x01, 0x54, 0x10, 0x32, 0x98,
  64. 0x74, 0, 7, 0x10, 0x85, 0x80, 0x00, 0x00, 0x4E, 0x00,
  65. 0x98, 0, 2, 0x3E, 0x07,
  66. GC9A01_CMD_TEARING_OFF, 0, 0,
  67. GC9A01_CMD_INVERT_OFF, 0, 0,
  68. GC9A01_CMD_SLEEP_OFF, 120, 0,
  69. GC9A01_CMD_DISPLAY_ON, 20, 0
  70. };
  71. // clang-format on
  72. // clang-format on
  73. qp_comms_bulk_command_sequence(device, gc9a01_init_sequence, sizeof(gc9a01_init_sequence));
  74. // Configure the rotation (i.e. the ordering and direction of memory writes in GRAM)
  75. const uint8_t madctl[] = {
  76. [QP_ROTATION_0] = GC9A01_MADCTL_BGR,
  77. [QP_ROTATION_90] = GC9A01_MADCTL_BGR | GC9A01_MADCTL_MX | GC9A01_MADCTL_MV,
  78. [QP_ROTATION_180] = GC9A01_MADCTL_BGR | GC9A01_MADCTL_MX | GC9A01_MADCTL_MY,
  79. [QP_ROTATION_270] = GC9A01_MADCTL_BGR | GC9A01_MADCTL_MV | GC9A01_MADCTL_MY,
  80. };
  81. qp_comms_command_databyte(device, GC9A01_SET_MEM_ACS_CTL, madctl[rotation]);
  82. return true;
  83. }
  84. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  85. // Driver vtable
  86. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  87. const struct tft_panel_dc_reset_painter_driver_vtable_t gc9a01_driver_vtable = {
  88. .base =
  89. {
  90. .init = qp_gc9a01_init,
  91. .power = qp_tft_panel_power,
  92. .clear = qp_tft_panel_clear,
  93. .flush = qp_tft_panel_flush,
  94. .pixdata = qp_tft_panel_pixdata,
  95. .viewport = qp_tft_panel_viewport,
  96. .palette_convert = qp_tft_panel_palette_convert,
  97. .append_pixels = qp_tft_panel_append_pixels,
  98. },
  99. .rgb888_to_native16bit = qp_rgb888_to_rgb565_swapped,
  100. .num_window_bytes = 2,
  101. .swap_window_coords = false,
  102. .opcodes =
  103. {
  104. .display_on = GC9A01_CMD_DISPLAY_ON,
  105. .display_off = GC9A01_CMD_DISPLAY_OFF,
  106. .set_column_address = GC9A01_SET_COL_ADDR,
  107. .set_row_address = GC9A01_SET_PAGE_ADDR,
  108. .enable_writes = GC9A01_SET_MEM,
  109. },
  110. };
  111. #ifdef QUANTUM_PAINTER_GC9A01_SPI_ENABLE
  112. // Factory function for creating a handle to the ILI9341 device
  113. painter_device_t qp_gc9a01_make_spi_device(uint16_t panel_width, uint16_t panel_height, pin_t chip_select_pin, pin_t dc_pin, pin_t reset_pin, uint16_t spi_divisor, int spi_mode) {
  114. for (uint32_t i = 0; i < GC9A01_NUM_DEVICES; ++i) {
  115. tft_panel_dc_reset_painter_device_t *driver = &gc9a01_drivers[i];
  116. if (!driver->base.driver_vtable) {
  117. driver->base.driver_vtable = (const struct painter_driver_vtable_t *)&gc9a01_driver_vtable;
  118. driver->base.comms_vtable = (const struct painter_comms_vtable_t *)&spi_comms_with_dc_vtable;
  119. driver->base.native_bits_per_pixel = 16; // RGB565
  120. driver->base.panel_width = panel_width;
  121. driver->base.panel_height = panel_height;
  122. driver->base.rotation = QP_ROTATION_0;
  123. driver->base.offset_x = 0;
  124. driver->base.offset_y = 0;
  125. // SPI and other pin configuration
  126. driver->base.comms_config = &driver->spi_dc_reset_config;
  127. driver->spi_dc_reset_config.spi_config.chip_select_pin = chip_select_pin;
  128. driver->spi_dc_reset_config.spi_config.divisor = spi_divisor;
  129. driver->spi_dc_reset_config.spi_config.lsb_first = false;
  130. driver->spi_dc_reset_config.spi_config.mode = spi_mode;
  131. driver->spi_dc_reset_config.dc_pin = dc_pin;
  132. driver->spi_dc_reset_config.reset_pin = reset_pin;
  133. return (painter_device_t)driver;
  134. }
  135. }
  136. return NULL;
  137. }
  138. #endif // QUANTUM_PAINTER_GC9A01_SPI_ENABLE