mi0283qt.c 6.7 KB

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