mi0283qt.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * DRM driver for Multi-Inno MI0283QT panels
  4. *
  5. * Copyright 2016 Noralf Trønnes
  6. */
  7. #include <linux/backlight.h>
  8. #include <linux/delay.h>
  9. #include <linux/gpio/consumer.h>
  10. #include <linux/module.h>
  11. #include <linux/property.h>
  12. #include <linux/regulator/consumer.h>
  13. #include <linux/spi/spi.h>
  14. #include <drm/drm_atomic_helper.h>
  15. #include <drm/drm_drv.h>
  16. #include <drm/drm_fbdev_dma.h>
  17. #include <drm/drm_gem_atomic_helper.h>
  18. #include <drm/drm_gem_dma_helper.h>
  19. #include <drm/drm_managed.h>
  20. #include <drm/drm_mipi_dbi.h>
  21. #include <drm/drm_modeset_helper.h>
  22. #include <video/mipi_display.h>
  23. #define ILI9341_FRMCTR1 0xb1
  24. #define ILI9341_DISCTRL 0xb6
  25. #define ILI9341_ETMOD 0xb7
  26. #define ILI9341_PWCTRL1 0xc0
  27. #define ILI9341_PWCTRL2 0xc1
  28. #define ILI9341_VMCTRL1 0xc5
  29. #define ILI9341_VMCTRL2 0xc7
  30. #define ILI9341_PWCTRLA 0xcb
  31. #define ILI9341_PWCTRLB 0xcf
  32. #define ILI9341_PGAMCTRL 0xe0
  33. #define ILI9341_NGAMCTRL 0xe1
  34. #define ILI9341_DTCTRLA 0xe8
  35. #define ILI9341_DTCTRLB 0xea
  36. #define ILI9341_PWRSEQ 0xed
  37. #define ILI9341_EN3GAM 0xf2
  38. #define ILI9341_PUMPCTRL 0xf7
  39. #define ILI9341_MADCTL_BGR BIT(3)
  40. #define ILI9341_MADCTL_MV BIT(5)
  41. #define ILI9341_MADCTL_MX BIT(6)
  42. #define ILI9341_MADCTL_MY BIT(7)
  43. static void mi0283qt_enable(struct drm_simple_display_pipe *pipe,
  44. struct drm_crtc_state *crtc_state,
  45. struct drm_plane_state *plane_state)
  46. {
  47. struct mipi_dbi_dev *dbidev = drm_to_mipi_dbi_dev(pipe->crtc.dev);
  48. struct mipi_dbi *dbi = &dbidev->dbi;
  49. u8 addr_mode;
  50. int ret, idx;
  51. if (!drm_dev_enter(pipe->crtc.dev, &idx))
  52. return;
  53. DRM_DEBUG_KMS("\n");
  54. ret = mipi_dbi_poweron_conditional_reset(dbidev);
  55. if (ret < 0)
  56. goto out_exit;
  57. if (ret == 1)
  58. goto out_enable;
  59. mipi_dbi_command(dbi, MIPI_DCS_SET_DISPLAY_OFF);
  60. mipi_dbi_command(dbi, ILI9341_PWCTRLB, 0x00, 0x83, 0x30);
  61. mipi_dbi_command(dbi, ILI9341_PWRSEQ, 0x64, 0x03, 0x12, 0x81);
  62. mipi_dbi_command(dbi, ILI9341_DTCTRLA, 0x85, 0x01, 0x79);
  63. mipi_dbi_command(dbi, ILI9341_PWCTRLA, 0x39, 0x2c, 0x00, 0x34, 0x02);
  64. mipi_dbi_command(dbi, ILI9341_PUMPCTRL, 0x20);
  65. mipi_dbi_command(dbi, ILI9341_DTCTRLB, 0x00, 0x00);
  66. /* Power Control */
  67. mipi_dbi_command(dbi, ILI9341_PWCTRL1, 0x26);
  68. mipi_dbi_command(dbi, ILI9341_PWCTRL2, 0x11);
  69. /* VCOM */
  70. mipi_dbi_command(dbi, ILI9341_VMCTRL1, 0x35, 0x3e);
  71. mipi_dbi_command(dbi, ILI9341_VMCTRL2, 0xbe);
  72. /* Memory Access Control */
  73. mipi_dbi_command(dbi, MIPI_DCS_SET_PIXEL_FORMAT, MIPI_DCS_PIXEL_FMT_16BIT);
  74. /* Frame Rate */
  75. mipi_dbi_command(dbi, ILI9341_FRMCTR1, 0x00, 0x1b);
  76. /* Gamma */
  77. mipi_dbi_command(dbi, ILI9341_EN3GAM, 0x08);
  78. mipi_dbi_command(dbi, MIPI_DCS_SET_GAMMA_CURVE, 0x01);
  79. mipi_dbi_command(dbi, ILI9341_PGAMCTRL,
  80. 0x1f, 0x1a, 0x18, 0x0a, 0x0f, 0x06, 0x45, 0x87,
  81. 0x32, 0x0a, 0x07, 0x02, 0x07, 0x05, 0x00);
  82. mipi_dbi_command(dbi, ILI9341_NGAMCTRL,
  83. 0x00, 0x25, 0x27, 0x05, 0x10, 0x09, 0x3a, 0x78,
  84. 0x4d, 0x05, 0x18, 0x0d, 0x38, 0x3a, 0x1f);
  85. /* DDRAM */
  86. mipi_dbi_command(dbi, ILI9341_ETMOD, 0x07);
  87. /* Display */
  88. mipi_dbi_command(dbi, ILI9341_DISCTRL, 0x0a, 0x82, 0x27, 0x00);
  89. mipi_dbi_command(dbi, MIPI_DCS_EXIT_SLEEP_MODE);
  90. msleep(100);
  91. mipi_dbi_command(dbi, MIPI_DCS_SET_DISPLAY_ON);
  92. msleep(100);
  93. out_enable:
  94. /* The PiTFT (ili9340) has a hardware reset circuit that
  95. * resets only on power-on and not on each reboot through
  96. * a gpio like the rpi-display does.
  97. * As a result, we need to always apply the rotation value
  98. * regardless of the display "on/off" state.
  99. */
  100. switch (dbidev->rotation) {
  101. default:
  102. addr_mode = ILI9341_MADCTL_MV | ILI9341_MADCTL_MY |
  103. ILI9341_MADCTL_MX;
  104. break;
  105. case 90:
  106. addr_mode = ILI9341_MADCTL_MY;
  107. break;
  108. case 180:
  109. addr_mode = ILI9341_MADCTL_MV;
  110. break;
  111. case 270:
  112. addr_mode = ILI9341_MADCTL_MX;
  113. break;
  114. }
  115. addr_mode |= ILI9341_MADCTL_BGR;
  116. mipi_dbi_command(dbi, MIPI_DCS_SET_ADDRESS_MODE, addr_mode);
  117. mipi_dbi_enable_flush(dbidev, crtc_state, plane_state);
  118. out_exit:
  119. drm_dev_exit(idx);
  120. }
  121. static const struct drm_simple_display_pipe_funcs mi0283qt_pipe_funcs = {
  122. DRM_MIPI_DBI_SIMPLE_DISPLAY_PIPE_FUNCS(mi0283qt_enable),
  123. };
  124. static const struct drm_display_mode mi0283qt_mode = {
  125. DRM_SIMPLE_MODE(320, 240, 58, 43),
  126. };
  127. DEFINE_DRM_GEM_DMA_FOPS(mi0283qt_fops);
  128. static const struct drm_driver mi0283qt_driver = {
  129. .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
  130. .fops = &mi0283qt_fops,
  131. DRM_GEM_DMA_DRIVER_OPS_VMAP,
  132. .debugfs_init = mipi_dbi_debugfs_init,
  133. .name = "mi0283qt",
  134. .desc = "Multi-Inno MI0283QT",
  135. .date = "20160614",
  136. .major = 1,
  137. .minor = 0,
  138. };
  139. static const struct of_device_id mi0283qt_of_match[] = {
  140. { .compatible = "multi-inno,mi0283qt" },
  141. {},
  142. };
  143. MODULE_DEVICE_TABLE(of, mi0283qt_of_match);
  144. static const struct spi_device_id mi0283qt_id[] = {
  145. { "mi0283qt", 0 },
  146. { },
  147. };
  148. MODULE_DEVICE_TABLE(spi, mi0283qt_id);
  149. static int mi0283qt_probe(struct spi_device *spi)
  150. {
  151. struct device *dev = &spi->dev;
  152. struct mipi_dbi_dev *dbidev;
  153. struct drm_device *drm;
  154. struct mipi_dbi *dbi;
  155. struct gpio_desc *dc;
  156. u32 rotation = 0;
  157. int ret;
  158. dbidev = devm_drm_dev_alloc(dev, &mi0283qt_driver,
  159. struct mipi_dbi_dev, drm);
  160. if (IS_ERR(dbidev))
  161. return PTR_ERR(dbidev);
  162. dbi = &dbidev->dbi;
  163. drm = &dbidev->drm;
  164. dbi->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
  165. if (IS_ERR(dbi->reset))
  166. return dev_err_probe(dev, PTR_ERR(dbi->reset), "Failed to get GPIO 'reset'\n");
  167. dc = devm_gpiod_get_optional(dev, "dc", GPIOD_OUT_LOW);
  168. if (IS_ERR(dc))
  169. return dev_err_probe(dev, PTR_ERR(dc), "Failed to get GPIO 'dc'\n");
  170. dbidev->regulator = devm_regulator_get(dev, "power");
  171. if (IS_ERR(dbidev->regulator))
  172. return PTR_ERR(dbidev->regulator);
  173. dbidev->backlight = devm_of_find_backlight(dev);
  174. if (IS_ERR(dbidev->backlight))
  175. return PTR_ERR(dbidev->backlight);
  176. device_property_read_u32(dev, "rotation", &rotation);
  177. ret = mipi_dbi_spi_init(spi, dbi, dc);
  178. if (ret)
  179. return ret;
  180. ret = mipi_dbi_dev_init(dbidev, &mi0283qt_pipe_funcs, &mi0283qt_mode, rotation);
  181. if (ret)
  182. return ret;
  183. drm_mode_config_reset(drm);
  184. ret = drm_dev_register(drm, 0);
  185. if (ret)
  186. return ret;
  187. spi_set_drvdata(spi, drm);
  188. drm_fbdev_dma_setup(drm, 0);
  189. return 0;
  190. }
  191. static void mi0283qt_remove(struct spi_device *spi)
  192. {
  193. struct drm_device *drm = spi_get_drvdata(spi);
  194. drm_dev_unplug(drm);
  195. drm_atomic_helper_shutdown(drm);
  196. }
  197. static void mi0283qt_shutdown(struct spi_device *spi)
  198. {
  199. drm_atomic_helper_shutdown(spi_get_drvdata(spi));
  200. }
  201. static int __maybe_unused mi0283qt_pm_suspend(struct device *dev)
  202. {
  203. return drm_mode_config_helper_suspend(dev_get_drvdata(dev));
  204. }
  205. static int __maybe_unused mi0283qt_pm_resume(struct device *dev)
  206. {
  207. drm_mode_config_helper_resume(dev_get_drvdata(dev));
  208. return 0;
  209. }
  210. static const struct dev_pm_ops mi0283qt_pm_ops = {
  211. SET_SYSTEM_SLEEP_PM_OPS(mi0283qt_pm_suspend, mi0283qt_pm_resume)
  212. };
  213. static struct spi_driver mi0283qt_spi_driver = {
  214. .driver = {
  215. .name = "mi0283qt",
  216. .of_match_table = mi0283qt_of_match,
  217. .pm = &mi0283qt_pm_ops,
  218. },
  219. .id_table = mi0283qt_id,
  220. .probe = mi0283qt_probe,
  221. .remove = mi0283qt_remove,
  222. .shutdown = mi0283qt_shutdown,
  223. };
  224. module_spi_driver(mi0283qt_spi_driver);
  225. MODULE_DESCRIPTION("Multi-Inno MI0283QT DRM driver");
  226. MODULE_AUTHOR("Noralf Trønnes");
  227. MODULE_LICENSE("GPL");