mi0283qt.c 7.1 KB

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