st7735r.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * DRM driver for Sitronix ST7735R panels
  4. *
  5. * Copyright 2017 David Lechner <david@lechnology.com>
  6. */
  7. #include <linux/delay.h>
  8. #include <linux/dma-buf.h>
  9. #include <linux/gpio/consumer.h>
  10. #include <linux/module.h>
  11. #include <linux/property.h>
  12. #include <linux/spi/spi.h>
  13. #include <video/mipi_display.h>
  14. #include <drm/drm_fb_helper.h>
  15. #include <drm/drm_gem_framebuffer_helper.h>
  16. #include <drm/tinydrm/mipi-dbi.h>
  17. #include <drm/tinydrm/tinydrm-helpers.h>
  18. #define ST7735R_FRMCTR1 0xb1
  19. #define ST7735R_FRMCTR2 0xb2
  20. #define ST7735R_FRMCTR3 0xb3
  21. #define ST7735R_INVCTR 0xb4
  22. #define ST7735R_PWCTR1 0xc0
  23. #define ST7735R_PWCTR2 0xc1
  24. #define ST7735R_PWCTR3 0xc2
  25. #define ST7735R_PWCTR4 0xc3
  26. #define ST7735R_PWCTR5 0xc4
  27. #define ST7735R_VMCTR1 0xc5
  28. #define ST7735R_GAMCTRP1 0xe0
  29. #define ST7735R_GAMCTRN1 0xe1
  30. #define ST7735R_MY BIT(7)
  31. #define ST7735R_MX BIT(6)
  32. #define ST7735R_MV BIT(5)
  33. static void jd_t18003_t01_pipe_enable(struct drm_simple_display_pipe *pipe,
  34. struct drm_crtc_state *crtc_state)
  35. {
  36. struct tinydrm_device *tdev = pipe_to_tinydrm(pipe);
  37. struct mipi_dbi *mipi = mipi_dbi_from_tinydrm(tdev);
  38. int ret;
  39. u8 addr_mode;
  40. DRM_DEBUG_KMS("\n");
  41. ret = mipi_dbi_poweron_reset(mipi);
  42. if (ret)
  43. return;
  44. msleep(150);
  45. mipi_dbi_command(mipi, MIPI_DCS_EXIT_SLEEP_MODE);
  46. msleep(500);
  47. mipi_dbi_command(mipi, ST7735R_FRMCTR1, 0x01, 0x2c, 0x2d);
  48. mipi_dbi_command(mipi, ST7735R_FRMCTR2, 0x01, 0x2c, 0x2d);
  49. mipi_dbi_command(mipi, ST7735R_FRMCTR3, 0x01, 0x2c, 0x2d, 0x01, 0x2c,
  50. 0x2d);
  51. mipi_dbi_command(mipi, ST7735R_INVCTR, 0x07);
  52. mipi_dbi_command(mipi, ST7735R_PWCTR1, 0xa2, 0x02, 0x84);
  53. mipi_dbi_command(mipi, ST7735R_PWCTR2, 0xc5);
  54. mipi_dbi_command(mipi, ST7735R_PWCTR3, 0x0a, 0x00);
  55. mipi_dbi_command(mipi, ST7735R_PWCTR4, 0x8a, 0x2a);
  56. mipi_dbi_command(mipi, ST7735R_PWCTR5, 0x8a, 0xee);
  57. mipi_dbi_command(mipi, ST7735R_VMCTR1, 0x0e);
  58. mipi_dbi_command(mipi, MIPI_DCS_EXIT_INVERT_MODE);
  59. switch (mipi->rotation) {
  60. default:
  61. addr_mode = ST7735R_MX | ST7735R_MY;
  62. break;
  63. case 90:
  64. addr_mode = ST7735R_MX | ST7735R_MV;
  65. break;
  66. case 180:
  67. addr_mode = 0;
  68. break;
  69. case 270:
  70. addr_mode = ST7735R_MY | ST7735R_MV;
  71. break;
  72. }
  73. mipi_dbi_command(mipi, MIPI_DCS_SET_ADDRESS_MODE, addr_mode);
  74. mipi_dbi_command(mipi, MIPI_DCS_SET_PIXEL_FORMAT,
  75. MIPI_DCS_PIXEL_FMT_16BIT);
  76. mipi_dbi_command(mipi, ST7735R_GAMCTRP1, 0x02, 0x1c, 0x07, 0x12, 0x37,
  77. 0x32, 0x29, 0x2d, 0x29, 0x25, 0x2b, 0x39, 0x00, 0x01,
  78. 0x03, 0x10);
  79. mipi_dbi_command(mipi, ST7735R_GAMCTRN1, 0x03, 0x1d, 0x07, 0x06, 0x2e,
  80. 0x2c, 0x29, 0x2d, 0x2e, 0x2e, 0x37, 0x3f, 0x00, 0x00,
  81. 0x02, 0x10);
  82. mipi_dbi_command(mipi, MIPI_DCS_SET_DISPLAY_ON);
  83. msleep(100);
  84. mipi_dbi_command(mipi, MIPI_DCS_ENTER_NORMAL_MODE);
  85. msleep(20);
  86. mipi_dbi_enable_flush(mipi);
  87. }
  88. static const struct drm_simple_display_pipe_funcs jd_t18003_t01_pipe_funcs = {
  89. .enable = jd_t18003_t01_pipe_enable,
  90. .disable = mipi_dbi_pipe_disable,
  91. .update = tinydrm_display_pipe_update,
  92. .prepare_fb = tinydrm_display_pipe_prepare_fb,
  93. };
  94. static const struct drm_display_mode jd_t18003_t01_mode = {
  95. TINYDRM_MODE(128, 160, 28, 35),
  96. };
  97. DEFINE_DRM_GEM_CMA_FOPS(st7735r_fops);
  98. static struct drm_driver st7735r_driver = {
  99. .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME |
  100. DRIVER_ATOMIC,
  101. .fops = &st7735r_fops,
  102. TINYDRM_GEM_DRIVER_OPS,
  103. .lastclose = drm_fb_helper_lastclose,
  104. .debugfs_init = mipi_dbi_debugfs_init,
  105. .name = "st7735r",
  106. .desc = "Sitronix ST7735R",
  107. .date = "20171128",
  108. .major = 1,
  109. .minor = 0,
  110. };
  111. static const struct of_device_id st7735r_of_match[] = {
  112. { .compatible = "jianda,jd-t18003-t01" },
  113. { },
  114. };
  115. MODULE_DEVICE_TABLE(of, st7735r_of_match);
  116. static const struct spi_device_id st7735r_id[] = {
  117. { "jd-t18003-t01", 0 },
  118. { },
  119. };
  120. MODULE_DEVICE_TABLE(spi, st7735r_id);
  121. static int st7735r_probe(struct spi_device *spi)
  122. {
  123. struct device *dev = &spi->dev;
  124. struct mipi_dbi *mipi;
  125. struct gpio_desc *dc;
  126. u32 rotation = 0;
  127. int ret;
  128. mipi = devm_kzalloc(dev, sizeof(*mipi), GFP_KERNEL);
  129. if (!mipi)
  130. return -ENOMEM;
  131. mipi->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
  132. if (IS_ERR(mipi->reset)) {
  133. DRM_DEV_ERROR(dev, "Failed to get gpio 'reset'\n");
  134. return PTR_ERR(mipi->reset);
  135. }
  136. dc = devm_gpiod_get(dev, "dc", GPIOD_OUT_LOW);
  137. if (IS_ERR(dc)) {
  138. DRM_DEV_ERROR(dev, "Failed to get gpio 'dc'\n");
  139. return PTR_ERR(dc);
  140. }
  141. mipi->backlight = tinydrm_of_find_backlight(dev);
  142. if (IS_ERR(mipi->backlight))
  143. return PTR_ERR(mipi->backlight);
  144. device_property_read_u32(dev, "rotation", &rotation);
  145. ret = mipi_dbi_spi_init(spi, mipi, dc);
  146. if (ret)
  147. return ret;
  148. /* Cannot read from Adafruit 1.8" display via SPI */
  149. mipi->read_commands = NULL;
  150. ret = mipi_dbi_init(&spi->dev, mipi, &jd_t18003_t01_pipe_funcs,
  151. &st7735r_driver, &jd_t18003_t01_mode, rotation);
  152. if (ret)
  153. return ret;
  154. spi_set_drvdata(spi, mipi);
  155. return devm_tinydrm_register(&mipi->tinydrm);
  156. }
  157. static void st7735r_shutdown(struct spi_device *spi)
  158. {
  159. struct mipi_dbi *mipi = spi_get_drvdata(spi);
  160. tinydrm_shutdown(&mipi->tinydrm);
  161. }
  162. static struct spi_driver st7735r_spi_driver = {
  163. .driver = {
  164. .name = "st7735r",
  165. .owner = THIS_MODULE,
  166. .of_match_table = st7735r_of_match,
  167. },
  168. .id_table = st7735r_id,
  169. .probe = st7735r_probe,
  170. .shutdown = st7735r_shutdown,
  171. };
  172. module_spi_driver(st7735r_spi_driver);
  173. MODULE_DESCRIPTION("Sitronix ST7735R DRM driver");
  174. MODULE_AUTHOR("David Lechner <david@lechnology.com>");
  175. MODULE_LICENSE("GPL");