ili9341.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * DRM driver for Ilitek ILI9341 panels
  4. *
  5. * Copyright 2018 David Lechner <david@lechnology.com>
  6. *
  7. * Based on mi0283qt.c:
  8. * Copyright 2016 Noralf Trønnes
  9. */
  10. #include <linux/backlight.h>
  11. #include <linux/delay.h>
  12. #include <linux/gpio/consumer.h>
  13. #include <linux/module.h>
  14. #include <linux/property.h>
  15. #include <linux/spi/spi.h>
  16. #include <drm/drm_atomic_helper.h>
  17. #include <drm/drm_drv.h>
  18. #include <drm/drm_fbdev_dma.h>
  19. #include <drm/drm_gem_atomic_helper.h>
  20. #include <drm/drm_gem_dma_helper.h>
  21. #include <drm/drm_managed.h>
  22. #include <drm/drm_mipi_dbi.h>
  23. #include <drm/drm_modeset_helper.h>
  24. #include <video/mipi_display.h>
  25. #define ILI9341_FRMCTR1 0xb1
  26. #define ILI9341_DISCTRL 0xb6
  27. #define ILI9341_ETMOD 0xb7
  28. #define ILI9341_PWCTRL1 0xc0
  29. #define ILI9341_PWCTRL2 0xc1
  30. #define ILI9341_VMCTRL1 0xc5
  31. #define ILI9341_VMCTRL2 0xc7
  32. #define ILI9341_PWCTRLA 0xcb
  33. #define ILI9341_PWCTRLB 0xcf
  34. #define ILI9341_PGAMCTRL 0xe0
  35. #define ILI9341_NGAMCTRL 0xe1
  36. #define ILI9341_DTCTRLA 0xe8
  37. #define ILI9341_DTCTRLB 0xea
  38. #define ILI9341_PWRSEQ 0xed
  39. #define ILI9341_EN3GAM 0xf2
  40. #define ILI9341_PUMPCTRL 0xf7
  41. #define ILI9341_MADCTL_BGR BIT(3)
  42. #define ILI9341_MADCTL_MV BIT(5)
  43. #define ILI9341_MADCTL_MX BIT(6)
  44. #define ILI9341_MADCTL_MY BIT(7)
  45. static void yx240qv29_enable(struct drm_simple_display_pipe *pipe,
  46. struct drm_crtc_state *crtc_state,
  47. struct drm_plane_state *plane_state)
  48. {
  49. struct mipi_dbi_dev *dbidev = drm_to_mipi_dbi_dev(pipe->crtc.dev);
  50. struct mipi_dbi *dbi = &dbidev->dbi;
  51. u8 addr_mode;
  52. int ret, idx;
  53. if (!drm_dev_enter(pipe->crtc.dev, &idx))
  54. return;
  55. DRM_DEBUG_KMS("\n");
  56. ret = mipi_dbi_poweron_conditional_reset(dbidev);
  57. if (ret < 0)
  58. goto out_exit;
  59. if (ret == 1)
  60. goto out_enable;
  61. mipi_dbi_command(dbi, MIPI_DCS_SET_DISPLAY_OFF);
  62. mipi_dbi_command(dbi, ILI9341_PWCTRLB, 0x00, 0xc1, 0x30);
  63. mipi_dbi_command(dbi, ILI9341_PWRSEQ, 0x64, 0x03, 0x12, 0x81);
  64. mipi_dbi_command(dbi, ILI9341_DTCTRLA, 0x85, 0x00, 0x78);
  65. mipi_dbi_command(dbi, ILI9341_PWCTRLA, 0x39, 0x2c, 0x00, 0x34, 0x02);
  66. mipi_dbi_command(dbi, ILI9341_PUMPCTRL, 0x20);
  67. mipi_dbi_command(dbi, ILI9341_DTCTRLB, 0x00, 0x00);
  68. /* Power Control */
  69. mipi_dbi_command(dbi, ILI9341_PWCTRL1, 0x23);
  70. mipi_dbi_command(dbi, ILI9341_PWCTRL2, 0x10);
  71. /* VCOM */
  72. mipi_dbi_command(dbi, ILI9341_VMCTRL1, 0x3e, 0x28);
  73. mipi_dbi_command(dbi, ILI9341_VMCTRL2, 0x86);
  74. /* Memory Access Control */
  75. mipi_dbi_command(dbi, MIPI_DCS_SET_PIXEL_FORMAT, MIPI_DCS_PIXEL_FMT_16BIT);
  76. /* Frame Rate */
  77. mipi_dbi_command(dbi, ILI9341_FRMCTR1, 0x00, 0x1b);
  78. /* Gamma */
  79. mipi_dbi_command(dbi, ILI9341_EN3GAM, 0x00);
  80. mipi_dbi_command(dbi, MIPI_DCS_SET_GAMMA_CURVE, 0x01);
  81. mipi_dbi_command(dbi, ILI9341_PGAMCTRL,
  82. 0x0f, 0x31, 0x2b, 0x0c, 0x0e, 0x08, 0x4e, 0xf1,
  83. 0x37, 0x07, 0x10, 0x03, 0x0e, 0x09, 0x00);
  84. mipi_dbi_command(dbi, ILI9341_NGAMCTRL,
  85. 0x00, 0x0e, 0x14, 0x03, 0x11, 0x07, 0x31, 0xc1,
  86. 0x48, 0x08, 0x0f, 0x0c, 0x31, 0x36, 0x0f);
  87. /* DDRAM */
  88. mipi_dbi_command(dbi, ILI9341_ETMOD, 0x07);
  89. /* Display */
  90. mipi_dbi_command(dbi, ILI9341_DISCTRL, 0x08, 0x82, 0x27, 0x00);
  91. mipi_dbi_command(dbi, MIPI_DCS_EXIT_SLEEP_MODE);
  92. msleep(100);
  93. mipi_dbi_command(dbi, MIPI_DCS_SET_DISPLAY_ON);
  94. msleep(100);
  95. out_enable:
  96. switch (dbidev->rotation) {
  97. default:
  98. addr_mode = ILI9341_MADCTL_MX;
  99. break;
  100. case 90:
  101. addr_mode = ILI9341_MADCTL_MV;
  102. break;
  103. case 180:
  104. addr_mode = ILI9341_MADCTL_MY;
  105. break;
  106. case 270:
  107. addr_mode = ILI9341_MADCTL_MV | ILI9341_MADCTL_MY |
  108. ILI9341_MADCTL_MX;
  109. break;
  110. }
  111. addr_mode |= ILI9341_MADCTL_BGR;
  112. mipi_dbi_command(dbi, MIPI_DCS_SET_ADDRESS_MODE, addr_mode);
  113. mipi_dbi_enable_flush(dbidev, crtc_state, plane_state);
  114. out_exit:
  115. drm_dev_exit(idx);
  116. }
  117. static const struct drm_simple_display_pipe_funcs ili9341_pipe_funcs = {
  118. DRM_MIPI_DBI_SIMPLE_DISPLAY_PIPE_FUNCS(yx240qv29_enable),
  119. };
  120. static const struct drm_display_mode yx240qv29_mode = {
  121. DRM_SIMPLE_MODE(240, 320, 37, 49),
  122. };
  123. DEFINE_DRM_GEM_DMA_FOPS(ili9341_fops);
  124. static const struct drm_driver ili9341_driver = {
  125. .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
  126. .fops = &ili9341_fops,
  127. DRM_GEM_DMA_DRIVER_OPS_VMAP,
  128. .debugfs_init = mipi_dbi_debugfs_init,
  129. .name = "ili9341",
  130. .desc = "Ilitek ILI9341",
  131. .date = "20180514",
  132. .major = 1,
  133. .minor = 0,
  134. };
  135. static const struct of_device_id ili9341_of_match[] = {
  136. { .compatible = "adafruit,yx240qv29" },
  137. { }
  138. };
  139. MODULE_DEVICE_TABLE(of, ili9341_of_match);
  140. static const struct spi_device_id ili9341_id[] = {
  141. { "yx240qv29", 0 },
  142. { }
  143. };
  144. MODULE_DEVICE_TABLE(spi, ili9341_id);
  145. static int ili9341_probe(struct spi_device *spi)
  146. {
  147. struct device *dev = &spi->dev;
  148. struct mipi_dbi_dev *dbidev;
  149. struct drm_device *drm;
  150. struct mipi_dbi *dbi;
  151. struct gpio_desc *dc;
  152. u32 rotation = 0;
  153. int ret;
  154. dbidev = devm_drm_dev_alloc(dev, &ili9341_driver,
  155. struct mipi_dbi_dev, drm);
  156. if (IS_ERR(dbidev))
  157. return PTR_ERR(dbidev);
  158. dbi = &dbidev->dbi;
  159. drm = &dbidev->drm;
  160. dbi->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
  161. if (IS_ERR(dbi->reset))
  162. return dev_err_probe(dev, PTR_ERR(dbi->reset), "Failed to get GPIO 'reset'\n");
  163. dc = devm_gpiod_get_optional(dev, "dc", GPIOD_OUT_LOW);
  164. if (IS_ERR(dc))
  165. return dev_err_probe(dev, PTR_ERR(dc), "Failed to get GPIO 'dc'\n");
  166. dbidev->backlight = devm_of_find_backlight(dev);
  167. if (IS_ERR(dbidev->backlight))
  168. return PTR_ERR(dbidev->backlight);
  169. device_property_read_u32(dev, "rotation", &rotation);
  170. ret = mipi_dbi_spi_init(spi, dbi, dc);
  171. if (ret)
  172. return ret;
  173. ret = mipi_dbi_dev_init(dbidev, &ili9341_pipe_funcs, &yx240qv29_mode, rotation);
  174. if (ret)
  175. return ret;
  176. drm_mode_config_reset(drm);
  177. ret = drm_dev_register(drm, 0);
  178. if (ret)
  179. return ret;
  180. spi_set_drvdata(spi, drm);
  181. drm_fbdev_dma_setup(drm, 0);
  182. return 0;
  183. }
  184. static void ili9341_remove(struct spi_device *spi)
  185. {
  186. struct drm_device *drm = spi_get_drvdata(spi);
  187. drm_dev_unplug(drm);
  188. drm_atomic_helper_shutdown(drm);
  189. }
  190. static void ili9341_shutdown(struct spi_device *spi)
  191. {
  192. drm_atomic_helper_shutdown(spi_get_drvdata(spi));
  193. }
  194. static struct spi_driver ili9341_spi_driver = {
  195. .driver = {
  196. .name = "ili9341",
  197. .of_match_table = ili9341_of_match,
  198. },
  199. .id_table = ili9341_id,
  200. .probe = ili9341_probe,
  201. .remove = ili9341_remove,
  202. .shutdown = ili9341_shutdown,
  203. };
  204. module_spi_driver(ili9341_spi_driver);
  205. MODULE_DESCRIPTION("Ilitek ILI9341 DRM driver");
  206. MODULE_AUTHOR("David Lechner <david@lechnology.com>");
  207. MODULE_LICENSE("GPL");